CakePHP 3.5 access Auth data in custom layout

#1

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

#2

Do you have a use statement for auth.

#3

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

#4

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

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.

$this->viewBuilder()->setLayoutPath('dog');
        

instead of

$this->viewBuilder()->setLayout('indextp');
 if whatever
    set a
else
  set b
#5

Hello,

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') ;
}
  else
{
    echo  $this->element('offlinenav') ;
}

?>

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

#6

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

#7

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

#8

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.