Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if the current page is the homepage with CakePhp?

How can I detect if the user is on the homepage of my website with CakePhp?

May I use $this->webroot?

The goal is to do something only if the current page is the homepage.

like image 354
Dacobah Avatar asked Aug 14 '13 08:08

Dacobah


3 Answers

Simply you can try this:

if ($this->request->here == '/') {
       // some code
}

Also it is good to read this part of documentation:

You can use CakeRequest to introspect a variety of things about the request. Beyond the detectors, you can also find out other information from various properties and methods.

$this->request->webroot contains the webroot directory.
$this->request->base contains the base path.
$this->request->here contains the full address to the current request
$this->request->query contains the query string parameters.
like image 102
Arash Mousavi Avatar answered Nov 01 '22 01:11

Arash Mousavi


You can find it by comparing the present page with webroot or base

if ($this->here == $this->webroot){ // this is home page }

OR

if ($this->here == $this->base.'/'){ // this is home page }
like image 31
Ravinder Reddy Avatar answered Oct 31 '22 23:10

Ravinder Reddy


You can get it properly by checking against params like below:

if($this->params['controller']=='homes' && $this->params['action']=='index')

in this way you can check for any page of cakephp on view side

like image 1
Jhanvi Avatar answered Nov 01 '22 01:11

Jhanvi