cakephpcakephp-1.3

Cakephp - get user id in controller when using Auth


It seems like a two-step process to get this,

    $u = $this->Auth->user();
    $uid = $u['User']['id'];

Isn't there a variable set somewhere once a user is logged in?


Solution

  • You can use $uid = $this->Auth->user('id');

    Check the api documentation: https://api.cakephp.org/1.3/class-AuthComponent.html#_user

    For everybody using the newer Cakephp v4.x: https://api.cakephp.org/4.4/class-Cake.Controller.Component.AuthComponent.html#user()