How do you echo out current URL in Cake\'s view?
Cakephp 3.x anywhere:
Router::reverse(Router::getRequest(),true)
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.
In CakePHP 3 $this->here
will be deprecated. The actual way is using this method:
Router::url($this->request->getRequestTarget())
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
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)
.
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