How to get the full current URL for CakePHP

How do you echo the current URL in Cake mode?

+48
php cakephp
Jul 26 2018-11-21T00:
source share
20 answers

You can do

From a view file:

<?php echo $this->here; ?> 

Which will give you an absolute url on behalf of the host ie / controller / action / params

or

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

which should provide you with the full hostname url.

+71
Jul 29 '11 at 3:18 a.m.
source share

I prefer this because if I do not mention the word “query”, my IDE gives a warning.

 <?php echo $this->request->here; ?> 

API Document: class-CakeRequest




Edit: To clarify all the 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/ 
+41
Aug 6 '13 at 8:56 on
source share
 <?php echo $_SERVER[ 'REQUEST_URI' ]; ?> 

EDIT: or,

 <?php echo $this->Html->url( null, true ); ?> 
+25
Jul 26 2018-11-21T00:
source share

The following Cake method is useful because you can grab the full current URL and change parts of it without manually parsing the $_SERVER[ 'REQUEST_URI' ] string, and then manually combine it back into a valid URL for output.

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

Easily modify specific parts of the current URL:
1) make a copy of the cake request object: $request_copy = $this->request

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

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

+8
Nov 15
source share

I know this post is a little outdated, and CakePHP versions have flourished since then. In the current version (2.1.x) of CakePHP and even in 1.3.x, if I'm not mistaken, you can get the current URL of the controller / view, for example:

 $this->params['url']; 

Although this method does NOT return parameters, it is convenient if you want to add parameters to the link when creating new URLs. For example, we have the current URL:

Projects / edit / 6

And we want to add a custom action of the c_action parameter with the remove_image value, you can use $this->params['url]; and combine it with an array of parameter pairs with key => parameters:

 echo $this->Html->link('remove image', array_merge($this->params['url'], array('c_action' => 'remove_image')); 

Using the method described above, we can add our custom parameters to the link and not call a long chain of parameters to create the URL, because $ this-> params ['url] only ever returns the URL of the control.

In the above example, we need to manually add the identifier 6 back to the URL, so that the final link layout will look like this:

 echo $this->Html->link('remove image', array_merge($this->params['url'], array($id,'c_action' => 'remove_image')); 

Where $ is is the identifier of the project, and you assigned it to the variable $ id at the controller level. The new URL will be as follows:

Projects / edit / 6 / c_action: remove_image

Sorry if this is not the least unrelated, but I came across this question when looking for a method to achieve the above and thought that others could benefit from it.

+4
Apr 02 '12 at 8:15
source share

for CakePHP 3:

 $this->Url->build(null, true) // full URL with hostname $this->Url->build(null) // /controller/action/params 
+4
Sep 08 '14 at 12:14
source share

Getting current url for CakePHP 3.x?

In your layout:

 <?php $here = $this->request->here(); $canonical = $this->Url->build($here, true); ?> 

You will receive the full URL of the current page, including query string parameters.

eg. http: //website.example/controller/action? param = value

You can use it in the canonical meta tag if you need to do some SEO.

 <link rel="canonical" href="<?= $canonical; ?>"> 
+3
Apr 05 '16 at 8:53 on
source share

Getting the current url is pretty 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 only need a relative path, use the following

 echo $this->here; 

OR

Ideally, Router :: url ("", true) should return the absolute URL of the current view, but it always returns a relative URL. So hack the absolute url -

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

To get FULL_BASE_URL check here

+2
Mar 19 '13 at 13:49 on
source share

In the request object, you have everything you need. To understand this:

 debug($this->request->url); 

and in your case

 $here = $this->request->url; 
+2
Mar 05 '14 at 13:40
source share

Cakephp 3.5:

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

The call to $ this-> request-> here () has been deprecated since 3.4 and will be removed in 4.0.0. Instead, you should use getRequestTarget ().

+2
Aug 14 '17 at 15:04 on
source share

Cake Way for 1.3 - use Router :: reverse:

Documentation Link

 $url = Router::reverse($this->params) echo $url; 

gives

 /Full/Path/From/Root/MyController/MyAction/passed1/named_param:bob/?param1=true&param2=27 
+1
Dec 13
source share

for cakephp3 +:

 $url = $this->request->scheme().'://'.$this->request->domain().$this->request->here(false); 

will receive, for example: http://bgq.dev/home/index?t44=333

+1
May 19 '16 at 3:17
source share

I use $this->here for the path to get the whole URL that you will need to do, as Juhan said, and use the $_SERVER . There is no need to use the Cake function for this.

0
Jul 27 '11 at 8:39
source share

All the previously proposed approaches did not satisfy my requirements for obtaining the full URL (as in qualified), for example. for use in emailing from a controller action. I also need the schema and hostname, and thus came across the following approach:

 <?php echo Router::url( array( $id ), true ) ?> 

Due to the fact that the controller controller of the router array and the action are saved, however, the identifier is missing and, therefore, should be provided here. The second argument true is actually asking for the host name, etc. For the full URL.

Using Router :: url () is available in any situation and, therefore, can also be used in view files.

0
Oct 21
source share

Yes, it is easily a FULL URL when working with the controller in CakePHP 1.3>

 <?php echo Router::url( array('controller'=>$this->params['controller'],'action'=>$this->params['action']), true ); 

Saludos

0
Apr 01 '14 at 19:42
source share

Cakephp 3.x anywhere:

 Router::reverse(Router::getRequest(),true) 
0
Jan 05 '16 at 18:56
source share

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 
0
Mar 07 '16 at 23:06
source share

Use HTML Helper

 <?php echo $this->Html->url($this->here, true); ?> 

It gives the full url that starts with http or https

0
Nov 23 '16 at 2:34
source share

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 the 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) ?> 
0
May 09 '17 at 18:14
source share

To get the full URL without parameters:

 echo $this->Html->url('/', true); 

will return http(s)://(www.)your-domain.com

0
May 11 '17 at 9:28 a.m.
source share



All Articles