Kohana 3 - Get URLs

Could you help me with the following questions. How to get:

absolute / relative current url

absolute / relative application URL

I could, of course, use my own php to get it, but I think it's better to use the ko3 functions.

Any idea how this works?

Thanks in advance!

+7
kohana kohana-3
source share
3 answers

I tried to make a controller that correctly displayed them. Let me know if any of them are disabled.

class Controller_Info extends Controller { public function action_index() { $uris = array ( 'page' => array ( 'a' => Request::instance()->uri(), 'b' => URL::base(TRUE, FALSE).Request::instance()->uri(), 'c' => URL::site(Request::instance()->uri()), 'd' => URL::site(Request::instance()->uri(), TRUE), ), 'application' => array ( 'a' => URL::base(), 'b' => URL::base(TRUE, TRUE), 'c' => URL::site(), 'd' => URL::site(NULL, TRUE), ), ); $this->request->headers['Content-Type'] = 'text/plain'; $this->request->response = print_r($uris, true); } public function action_version() { $this->request->response = 'Kohana version: '.Kohana::VERSION; } public function action_php() { phpinfo(); } } 

Outputs the following:

 Array ( [page] => Array ( [a] => info/index [b] => /kohana/info/index [c] => /kohana/info/index [d] => http://localhost/kohana/info/index ) [application] => Array ( [a] => /kohana/ [b] => http://localhost/kohana/ [c] => /kohana/ [d] => http://localhost/kohana/ ) ) 

From a technical point of view, this is actually only the first URL of the page, which is the real relative URL, since all the others either start with / or http:// .


I need to get the URL for the current page, so I decided to extend the URL class. Thought I could share it here. Let me know what you think :)

 /** * Extension of the Kohana URL helper class. */ class URL extends Kohana_URL { /** * Fetches the URL to the current request uri. * * @param bool make absolute url * @param bool add protocol and domain (ignored if relative url) * @return string */ public static function current($absolute = FALSE, $protocol = FALSE) { $url = Request::instance()->uri(); if($absolute === TRUE) $url = self::site($url, $protocol); return $url; } } echo URL::current(); // controller/action echo URL::current(TRUE); // /base_url/controller/action echo URL::current(TRUE, TRUE); // http://domain/base_url/controller/action 
+13
source share

Don't you mean: Kohana_Request :: detect_uri ()?

+7
source share

Absolute / relative current URL:

 // outputs 'http://www.example.com/subdir/controller/action' echo URL::site(Request::detect_uri(),true)); // outputs '/subdir/controller/action' echo URL::site(Request::detect_uri()); 

Absolute / relative current application URL:

 // outputs 'http://www.example.com/subdir/' echo URL::site(NULL, TRUE); // outputs '/subdir/' echo URL::site(); 

Hope this helps

+2
source share

All Articles