Kohana 3 - Get URLs
Asked Answered
D

3

7

could you help me with following questions. How do i get the:

absolute/relative current url

absolute/relative application url

I could of course use native php to get it but i think i should rather use ko3 functions.

Any idea how that works?

Thanks in advance!

Didier answered 16/5, 2010 at 12:23 Comment(0)
F
13

Tried to make a controller that outputted them all correctly. Let me know if any of them are off.

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 this:

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/
        )
)

Technically speaking, it's actually only the first page url that is a real relative url, since all the others either start with / or http://.


Needed to get the url for the current page myself, so 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
Fortson answered 16/5, 2010 at 12:47 Comment(1)
on Kohana 3.1+ you need to pass a string ('http', 'https') or a Request object to the $protocol parameter in URL::site. And if you want to preserve the query string you can add URL::query() at the end.Roadability
U
7

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

Umberto answered 13/2, 2011 at 15:26 Comment(1)
Cheers twicejr. Using Request::detect_uri() is perfect.Glia
S
2

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 it helps

Sophistry answered 6/11, 2012 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.