PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, March 13, 2022

[FIXED] CakePHP3 get user in default.ctp

 March 13, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

I work on CakePHP 3 project. I want to display username of the logged user from default.ctp so I tried this:

//in default.ctp   
 $user = $this->Session->read('Auth.User');
    if(!empty($user)) {
        echo 'Hi ', $user['user_name'];
    }

It doesn't work and I found other solution which tells me to put the current user in session from AppController it doesn't work either.

// in AppController      
function beforeFilter(){
  $user = $this->Session->read('Auth.User');
  $this->set('username', $user['username']);
}

and in default.ctp:

<?=$username?>

How can I make this work?


Solution

<?php   $session = $this->request->session(); // less than 3.5
        // $session = $this->request->getSession(); // 3.5 or more
        $user_data = $session->read('Auth.User');
        if(!empty($user_data)){
            print_r($user_data);
  }?>

try this hope it help's..



Answered By - Vivek Singh
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing