在Cake
PHP 2.x中你可以做到
AuthComponent::user()
在View中从Auth组件获取数据.在CakePHP 3.0beta3中,它抛出:
"Error: Class 'AuthComponent' not found"
蛋糕3.5
在AppController中:
public function beforeRender(Event $event) { .... $this->set('Auth',$this->Auth); }
在.ctp模板中:
<?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 } ?>