Category: Views

Q: CakePHP access user login status in view

I need CakePHP 3 to access user login status (is logged in or not) in view .ctp file. I'm unable to use AuthComponent or SessionComponent to retrieve user status. I wonder if there is a SessionHelp that is able to access user status in front view.

Here is my code:

//from $this->Auth->setUser(); not working
<?php if ($user): ?>
    Welcome <?php echo $user['name']; ?>!
<?php endif; ?>

//not working
<?php if ($this->Auth->identify()): ?>
    Welcome <?php echo $username; ?>!
<?php endif; ?>

//also not working
<?php if ($this->Auth->user()): ?>
    Welcome <?php echo $username; ?>!
<?php endif; ?>

//works ( set user from controller, every action )
<?php if ($user): ?>
    Welcome <?php echo $user['name']; ?>!
<?php endif; ?>

I've tried many ways but still unable to find a solution.

2 Answers

Version: 3.3

Try to setUser() on login:

In your UsersController.php

public function login() {

    if ($this->request->is('post')) {

            $user = $this->Auth->identify();
            if ($user) {


            } else {

                $this->redirect(['controller' => 'html', 'action' => 'login']);

    $this->autoRender = false;

Then you can access $user in views.



In view and when a user is logged in:


Created: 4 Oct '16

Last Reply: 7 Oct '16

Replies: 2

Views: 1662

Votes: 0

Welcome to Aero Coding!

Aero Coding is a CakePHP-focused Q&A community for professional and enthusiast cake bakers. It's built and run by you as part of the community.

Join Now Tour

Download Cakephp

Start baking your own CakePHP application!

Cakephp All Versions