7

你能帮我解决以下问题吗?我如何获得:

绝对/相对当前网址

绝对/相对应用程序 url

我当然可以使用本机 php 来获取它,但我认为我宁愿使用 ko3 函数。

知道它是如何工作的吗?

提前致谢!

4

3 回答 3

13

试图制作一个能正确输出它们的控制器。让我知道他们中的任何一个是否关闭。

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();
    }

}

输出这个:

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

从技术上讲,实际上只有第一个页面 url 才是真正的相对 url,因为所有其他页面都以/或开头http://


需要自己获取当前页面的url,所以决定扩展url类。以为可以在这里分享。让我知道你的想法 :)

/**
 * 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
于 2010-05-16T12:47:34.793 回答
7

你不是说: Kohana_Request::detect_uri() 吗?

于 2011-02-13T15:26:18.870 回答
2

绝对/相对当前 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());

绝对/相对当前应用程序 URL:

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

// outputs '/subdir/'
echo URL::site();

希望能帮助到你

于 2012-11-06T11:26:04.573 回答