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

Wednesday, January 5, 2022

[FIXED] CakePHP 3.x - AuthComponent::user() in View

 January 05, 2022     cakephp, cakephp-3.0     No comments   

Issue

In CakePHP 2.x you could do

 AuthComponent::user()

in View to get data from Auth component. In CakePHP 3.0beta3 it throws:

 "Error: Class 'AuthComponent' not found"

Is there a simple way to get data from AuthComponent in View?


Solution

Cake 3.5

In AppController:

public function beforeRender(Event $event) {
    ....

    $this->set('Auth', $this->Auth);
}

In .ctp template:

<?php if (!$Auth->user()) { ?>
    <a class="login" href="<?php echo $this->Url->build($Auth->getConfig('loginAction')); ?>">Login</a>
<?php } else { ?>
    <div class="name"><?php echo h($Auth->user('name')); ?></div>
<?php } ?>


Answered By - AndreyP
  • 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