CakePHP 3.5 access Auth data in custom layout

Hi there,

I’m using a custom navbar for an authenticated user and another for unauthenticated users.

I’m using a custom error.ctp template and i want to test if the person who accesses the page is authenticated or not.

Apparently, this custom template does not have access to auth variables, the test is always negative even with an authenticated user.

My test is `

<?php if (isset($authName)) //test failed { echo $this->element('onlinenav') ; } else { echo $this->element('offlinenav') ; } ?>`

Thanks for any help

Do you have a use statement for auth.

No but in my others layouts i dont use anything and I can use the auth data

[quote=“christ57, post:1, topic:6045”]
<?php if (isset($authName))

Is ths part working:

<?php if (isset($authName))

Make that happen first. Can you perhaps pre-check in controller, just curious. Then pre load correct parameters in controller.


instead of

 if whatever
    set a
  set b


No, nothing works but more informations :

In a controller, i test if an article exist with a query , if not

throw new NotFoundException(__('this page doesn't exist.'));

In error400.ctp, i’m calling

$this->layout = 'error';

I want to test, in this ‘error.ctp’ ,this file that is in the folder /Template/Layout/, if the person who accesses it is connected.

I tested with

<?php if (isset($authName)) // test de l'authentification
    echo  $this->element('onlinenav') ;
    echo  $this->element('offlinenav') ;


This test works with every others layout except this one. I need this to work in debug mode -> false

If the navbar for not authenticated users doesn’t require Authentication then Auth would not apply there.

So there is no solution, i will find another way , thanks a lot

Should have nothing to do with an error, simply if not authenticated, after a login attempt you could just redirect to the nav for non-authenticated. Or 15 other ways.

But what if it is a authenticated user, and just enters a wrong password by mistake.

You need to work out varoius logic here…

Without seeing all code, it’s hard to visualize what another developer is doing.

To simplify things for you, you could have a “main page” with one link for someone required to sign in. And another link to the page with nav where the user is not required to sign in.