Kohana 3 - 获取 URL

发布于 2024-09-02 02:24:35 字数 501 浏览 6 评论 0原文

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

绝对/相对当前网址

绝对/相对应用程序 URL

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

知道这是如何运作的吗?

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花似海 2024-09-09 02:24:35

试图制作一个能够正确输出它们的控制器。如果其中任何一个已关闭,请告诉我。

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

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
橙幽之幻 2024-09-09 02:24:35

你不就是想说:
Kohana_Request::detect_uri() ?

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

百思不得你姐 2024-09-09 02:24:35

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

希望有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文