How do you echo out current URL in Cake's view?
25 Answers
You can do either
From a view file:
<?php echo $this->request->here() ?>">
Which will give you the absolute url from the hostname i.e. /controller/action/params
Or
<?php echo Router::url(null, true) ?>
which should give you the full url with the hostname.

- 153
- 1
- 4
- 19

- 4,012
- 22
- 18
-
29Side note from my experience (CakePHP 2+, not sure about the rest...) - you should use `Router::url(null, true)` instead of using `$this->here`. The reason is that if you ever have to configure a hardset base path $this->here will contain it already and your links will be double routed and broken. Just wanted to pass that along - confused the hell out of me tonight. – jocull Jan 20 '12 at 03:17
-
3Also, in cake2.x `$this->here` would not be enough anymore. You would need to use `$this->here()` as this also appends the query string (and thus would then form the complete url). – mark Jan 16 '13 at 09:08
-
2@mark, what version are you on in 2.x? I'm in 2.4.5 and I get `PHP Fatal error: Call to undefined method MyController::here()` when I try to use the `here()` method. – Tyler Collier Jul 29 '14 at 16:10
-
5Probably $this->request->here() ;) – mark Jul 29 '14 at 17:04
-
Please note that this doesn't contain the request URI – Alonso Urbano Nov 08 '17 at 18:40
-
Personally I needed the URL without the base buth with any parameters passed to the controller. A way to solve that is using `$this->getRequest()->getUri()->getPath()` (CakePHP 3 and 4). – Axel Köhler Jan 19 '22 at 10:09
I prefer this, because if I don't mention "request" word, my IDE gives warning.
<?php echo $this->request->here; ?>
API Document: class-CakeRequest
Edit: To clarify all options
Current URL: http://example.com/en/controller/action/?query=12
// Router::url(null, true)
http://example.com/en/controller/action/
// Router::url(null, false)
/en/controller/action/
// $this->request->here
/en/controller/action/
// $this->request->here()
/en/controller/action/?query=12
// $this->request->here(false)
/en/controller/action/?query=12
// $this->request->url
en/controller/action
// $_SERVER["REQUEST_URI"]
/en/controller/action/?query=12
// strtok($_SERVER["REQUEST_URI"],'?');
/en/controller/action/

- 5,610
- 4
- 42
- 60

- 33,518
- 47
- 192
- 272
-
-
My CakePHP project runs on the `localhost://projectname` URL (`projectname` folder in LAMPP stack), but using `$_SERVER["REQUEST_URI"]` doesn't include the `prohjectname` part. Any way to fix that? – Axel Köhler Jun 17 '21 at 16:58
<?php echo $_SERVER[ 'REQUEST_URI' ]; ?>
EDIT: or,
<?php echo $this->Html->url( null, true ); ?>

- 32,902
- 20
- 89
- 102
-
4There's no "Cake way" for everything that can be done simply with basic PHP :) I guess you could do `echo $this->Html->url( null, true );` – JJJ Jul 26 '11 at 22:09
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)
.

- 4,851
- 33
- 30
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.

- 2,510
- 1
- 29
- 17
-
-
2Or `$this->getRequest()->getRequestTarget()` to be even more deprecation-proof :D – ᴍᴇʜᴏᴠ Sep 07 '20 at 11:32
I know this post is a little dated, and CakePHP versions have flourished since. In the current (2.1.x) version of CakePHP and even in 1.3.x if I am not mistaken, one can get the current controller/view url like this:
$this->params['url'];
While this method does NOT return the parameters, it is handy if you want to append parameters to a link when building new URL's. For example, we have the current URL:
projects/edit/6
And we want to append a custom parameter action called c_action with a value of remove_image, one could make use of $this->params['url];
and merge it with an array of custom parameter key => value pairs:
echo $this->Html->link('remove image', array_merge($this->params['url'], array('c_action' => 'remove_image'));
Using the above method we are able to append our custom parameters to the link and not cause a long chain on parameters to build up on the URL, because $this->params['url] only ever returns the controll action URL.
In the above example we'd need to manually add the ID of 6 back into the URL, so perahaps the final link build would be like this:
echo $this->Html->link('remove image', array_merge($this->params['url'], array($id,'c_action' => 'remove_image'));
Where $is is a the ID of the project and you would have assigned it to the variable $id at controller level. The new URL will then be:
projects/edit/6/c_action:remove_image
Sorry if this is in the slightest unrelated, but I ran across this question when searching for a method to achieve the above and thought others may benefit from it.

- 2,026
- 4
- 24
- 36
-
You can just do `echo $this->Html->link('remove image', array('action' => 'remove_image'))`. I'm not sure if that is a functionality from later than 2.1.x, but that should work. Unless you overwrite the controller key in the array, the current controller will be used. – Jelmer Dec 14 '13 at 18:54
-
or `$this->request->url` for url including `query` and `named` params – Fr0zenFyr Jul 10 '19 at 07:28
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

- 1
- 1

- 18,333
- 14
- 83
- 102
for CakePHP 3:
$this->Url->build(null, true) // full URL with hostname
$this->Url->build(null) // /controller/action/params

- 2,223
- 15
- 17
Getting current URL for CakePHP 3.x ?
In your layout :
<?php
$here = $this->request->here();
$canonical = $this->Url->build($here, true);
?>
You will get the full URL of the current page including query string parameters.
e.g. http://website.example/controller/action?param=value
You can use it in a meta tag canonical if you need to do some SEO.
<link rel="canonical" href="<?= $canonical; ?>">
-
1Note: here() has been deprecated as of CakePHP 3.4.0 in favor of getRequestTarget() – Phantom Watson Jul 28 '18 at 22:22
In the request object you have everything you need. To understand it:
debug($this->request->url);
and in your case
$here = $this->request->url;

- 7,647
- 3
- 41
- 71
To get the full URL without parameters:
echo $this->Html->url('/', true);
will return http(s)://(www.)your-domain.com

- 1,483
- 1
- 13
- 26
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
More info you can read here: https://pritomkumar.blogspot.com/2017/04/how-to-get-complete-current-url-for.html

- 682
- 7
- 14
The simplest way I found is it that includes host/path/query and
works in Controllers
(Cakephp 3.4
):
Cake\View\Helper\UrlHelper::build($this->request->getRequestTarget());
which returns something like this (we use it as login callback url) :
http://192.168.0.57/archive?melkId=12

- 3,520
- 24
- 29
The Cake way for 1.3 is to use Router::reverse:
$url = Router::reverse($this->params)
echo $url;
yields
/Full/Path/From/Root/MyController/MyAction/passed1/named_param:bob/?param1=true¶m2=27

- 9,551
- 5
- 42
- 46
for CakePHP 3.x You can use UrlHelper
:
$this->Url->build(null, true) // output http://somedomain.com/app-name/controller/action/params
$this->Url->build() // output /controller/action/params
Or you can use PaginatorHelper
(in case you want to use it in javascript or ...):
$this->Paginator->generateUrl() // returns a full pagination URL without hostname
$this->Paginator->generateUrl([],null,true) // returns a full pagination URL with hostname

- 49
- 1
- 8
for cakephp3+:
$url = $this->request->scheme().'://'.$this->request->domain().$this->request->here(false);
will get eg: http://bgq.dev/home/index?t44=333

- 47
- 5
In View:
Blank URL: <?php echo $this->Html->Url('/') ?>
Blank Full Url: <?php echo $this->Html->Url('/', true) ?>
Current URL: <?php echo $this->Html->Url($this->here) ?>
Current Full URL: <?php echo $this->Html->Url($this->here, true) ?>
In Controller
Blank URL: <?php echo Router::url('/') ?>
Blank Full Url: <?php echo Router::url('/', true) ?>
Current URL: <?php echo Router::url($this->request->here()) ?>
Current Full URL: <?php echo Router::url($this->request->here(), true) ?>

- 76
- 1
- 1
- 6
All previously proposed approaches didn't satisfy my requirements for getting a complete URL (complete as in qualified) e.g. to be used in an email send from controller action. I need the scheme and hostname as well then, and thus stumbled over the following approach:
<?php echo Router::url( array( $id ), true ) ?>
Due to providing router array current controller and action is kept, however id isn't and thus has to be provided here again. Second argument true
is actually requesting to prepend hostname etc. for getting full URL.
Using Router::url() is available in every situation and thus can be used in view files as well.

- 431
- 2
- 4
- 14
Yes, is easy FULL URL in Controler Work in CakePHP 1.3 >
<?php echo Router::url( array('controller'=>$this->params['controller'],'action'=>$this->params['action']), true );
Saludos

- 29
- 1
- 1
Use Html helper
<?php echo $this->Html->url($this->here, true); ?>
It'll produce the full url which'll started from http or https

- 530
- 9
- 24
In CakePHP 3 $this->here
will be deprecated. The actual way is using this method:
Router::url($this->request->getRequestTarget())

- 168,389
- 15
- 48
- 91

- 126
- 5
For CakePHP 4.*
echo $this->Html->link(
'Dashboard',
['controller' => 'Dashboards', 'action' => 'index', '_full' => true]
);

- 755
- 1
- 12
- 27
If you want to return the base path, you can check that the Router
class is using Configure::read ('App.fullBaseUrl')
. So if you are a fan of hexagonal architecture, you can create a Cake implementation for creating URLs that will only use Configure
from all CakePHP framework.

- 109
- 1
- 4
I use $this->here
for the path, to get the whole URL you'll have to do as Juhana said and use the $_SERVER
variables. There's no need to use a Cake function for this.

- 14,682
- 4
- 50
- 74