Is this the best approach to show a sidebar depending

I’m trying to show a sidebar in other pages except, home page, and two other pages,
I’m asking, looking at the code below, am I go about it the recommended way or you have some suggestion for me. Thank you

<?php if (!in_array($this->getRequest()->getParam('controller'), ['Pages', 'Users', 'Another page'])) echo $this->element('sidebar'); echo $this->fetch('user_sidebar'); ?>

$this->getRequest()->getParam('controller') gives you ONLY the name of the used controller, not the action nor other params.

So if you have 3 static pages which are all handled via the PagesController::display method you will need something like:

$request = $this->getRequest();
$currentController = $request->getParam('controller');
$currentAction = $request->getParam('action');
$pass = $request->getParam('pass');

if($currentController === 'Pages' && currentAction === 'display'){
    if ($pass === 'home') {
        // do something here
    } else if ($pass === 'other') {
        // do something else

Why not create another layout ?

Or set a “no_sidebar” variable to true in those few exceptions, and show it only if that’s not set? There are many ways to attack this.

this is how I do my sidebars
First adding a fetch in you main layout


Then simple add view blocks on your templates. Templates without view block sidebar will not load a sidebar.

echo $this->element('sidebar/main');

but if you need to check user roles? then you can just make the role as a variable

echo $this->element('sidebar/sidebar'.$role);