Kohana 3 - 获取网址

n00*_*00b 7 kohana kohana-3

你能帮我解决下面的问题.我怎么得到:

绝对/相对当前网址

绝对/相对应用网址

我当然可以使用本机php来获取它,但我认为我应该使用ko3函数.

知道它是如何工作的吗?

提前致谢!

Svi*_*ish 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();
    }

}
Run Code Online (Sandbox Code Playgroud)

输出:

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/
        )
)
Run Code Online (Sandbox Code Playgroud)

从技术上来说,它实际上是仅在第一页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
Run Code Online (Sandbox Code Playgroud)

  • 在Kohana 3.1+上,您需要将一个字符串('http','https')或一个Request对象传递给URL :: site中的$ protocol参数.如果要保留查询字符串,可以在末尾添加URL :: query(). (2认同)

twi*_*ejr 7

你不只是意味着:Kohana_Request :: detect_uri()?