How to get complete current url for Cakephp

前端 未结 24 1217
长情又很酷
长情又很酷 2020-12-08 00:10

How do you echo out current URL in Cake\'s view?

相关标签:
24条回答
  • 2020-12-08 00:27

    Cakephp 3.x anywhere:

    Router::reverse(Router::getRequest(),true)
    
    0 讨论(0)
  • 2020-12-08 00:29

    Cakephp 3.5:

    echo $this->Url->build($this->getRequest()->getRequestTarget());
    

    Calling $this->request->here() is deprecated since 3.4, and will be removed in 4.0.0. You should use getRequestTarget() instead. $this->request is also deprecated, $this->getRequest() should be used.

    0 讨论(0)
  • 2020-12-08 00:29

    In CakePHP 3 $this->here will be deprecated. The actual way is using this method:

    Router::url($this->request->getRequestTarget())
    
    0 讨论(0)
  • 2020-12-08 00:31

    Getting the current URL is fairly straight forward in your view file

    echo Router::url($this->here, true);
    

    This will return the full url http://www.example.com/subpath/subpath

    If you just want the relative path, use the following

    echo $this->here;
    

    OR

    Ideally Router::url(“”, true) should return an absolute URL of the current view, but it always returns the relative URL. So the hack to get the absolute URL is

    $absolute_url  = FULL_BASE_URL + Router::url(“”, false);
    

    To get FULL_BASE_URL check here

    0 讨论(0)
  • 2020-12-08 00:32

    The following "Cake way" is useful because you can grab the full current URL and modify parts of it without manually having to parse out the $_SERVER[ 'REQUEST_URI' ] string and then manually concatenating it back into a valid url for output.

    Full current url:
    Router::reverse($this->request, true)

    Easily modifying specific parts of current url:
    1) make a copy of Cake's request object: $request_copy = $this->request

    2) Then modify $request_copy->params and/or $request_copy->query arrays

    3) Finally: $new_url = Router::reverse($request_copy, true).

    0 讨论(0)
  • 2020-12-08 00:32

    After a few research, I got this as perfect Full URL for CakePHP 3.*

    $this->request->getUri();
    

    the Full URL will be something like this

    http://example.com/books/edit/12

    More info you can read here: https://pritomkumar.blogspot.com/2017/04/how-to-get-complete-current-url-for.html

    0 讨论(0)
提交回复
热议问题