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

7

6

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.

Pecksniffian answered 14/8, 2013 at 8:8 Comment(1)
I don't know about cakephp but __file__ will output the path of the file and you can check if the path os same as path of homepage..Chatoyant
E
10

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.
Entangle answered 14/8, 2013 at 9:1 Comment(3)
Thank you! I discovered a new variable)Monopteros
This will not work under sub-folder e.g. http://localhost/cakephp/. In that case you can try this answerOurs
This is not a standard solution, what if you have multiple languages (/fr) .Hoyos
W
4

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 }
Woodhead answered 26/5, 2014 at 10:33 Comment(0)
P
1

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

Parallelize answered 14/8, 2013 at 13:9 Comment(0)
H
0

You can use $this->request->query['page'] to determine where you are,

if ( $this->request->query['page'] == '/' ){
   //do something
}

Edit:

check $this->request object using echo debug($this->request), it contains many informations you can use. Here is a sample of what you get:

object(CakeRequest) {
    params => array(
        'plugin' => null,
        'controller' => 'pages',
        'action' => 'display',
        'named' => array(),
        'pass' => array(
            (int) 0 => 'home'
        )
    )
    data => array()
    query => array()
    url => false
    base => ''
    webroot => '/'
    here => '/'
}
Halifax answered 14/8, 2013 at 8:51 Comment(2)
I tried it but it's not working. I suppose $this->request->query['page'] mean you have a querystring. But my url is more like www.example.com than www.example.com?page=...Pecksniffian
See my edit, you will probably find what you need in $this->request objectHalifax
F
0

Assuming you are going to do something from the AppController, it's best to see if the current controller/action pair is the one you define as "homepage" (as Cake can route a user anywhere from the '/' route and you probably still want the logic to be triggered when the action is called directly with the full /controller/action URI and not just on /). In your AppController just add a check:

if ($this->name == 'Foo' && $this->action == 'bar') {
    // Do your stuff here, like
    echo 'Welcome home!';
}

That way it will trigger whenever the bar action is requested from the FooController. You can obviously also put this logic in the specific controller action itself (and that might make more sense since it's less overhead).

Filomenafiloplume answered 14/8, 2013 at 8:52 Comment(0)
H
0

If your home page is home.ctp as mentionned by the cakePHP convention. In PagesController, you can change the display function to look like :

(added code starts at the comment /* Custom code start*/ )

public function display()
{
    $path = func_get_args();

    $count = count($path);
    if (!$count) {
        return $this->redirect('/');
    }
    $page = $subpage = null;

    if (!empty($path[0])) {
        $page = $path[0];
    }
    if (!empty($path[1])) {
        $subpage = $path[1];
    }
    /* Custom code start*/
    if("home"==$page){
        // your code here
    }
    /* Custom code end*/
    $this->set(compact('page', 'subpage'));

    try {
        $this->render(implode('/', $path));
    } catch (MissingTemplateException $e) {
        if (Configure::read('debug')) {
            throw $e;
        }
        throw new NotFoundException();
    }
}
Hoyos answered 20/3, 2017 at 2:56 Comment(0)
T
0

The way I achieved that was by using $this->params. If you use print_r($this->params);, you will see the content of that variable for you. It will return an array. You will see the difference of when you are versus when you are not in the home page. You will have to use one of the keys in $this->params to make your evaluations with an if statement. That was how I achieved it. Maybe you can find this approach useful too.

Tiffinytiffy answered 11/10, 2019 at 2:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.