CakePHP check if user is logged in inside a view

前端 未结 12 1927
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 13:00

I have the following code:

    Auth->user())
    {
        echo $this->element(\'header\');
    }
    else
    {
        e         


        
12条回答
  •  Happy的楠姐
    2020-12-24 13:23

    You need to set the user details from a controller, preferably the AppController which is inherited by all controllers across your site. Create/amend your app_controller.php to contain this beforeFilter(); method.

    Auth->user();
        $this->set(compact('user'));
    }
    

    This will set a var called $user to the views which will be empty if the user is not logged in, or contain their data if they are.

提交回复
热议问题