2015-04-04 70 views
1

我是一个新手,CakePHP的..我直接从3.0CakePHP的3.0博客教程认证错误

启动它,我也跟着教程BLOG 1,2和现在所面临的博客教程错误 - Authentication and Authorization

// src/Controller/UsersController.php 
public function beforeFilter(Event $event) 
{ 
parent::beforeFilter($event); 
// Allow users to register and logout. 
// You should not add the "login" action to allow list. Doing so would 
// cause problems with normal functioning of AuthComponent. 
$this->Auth->allow(['add', 'logout']); 
} 

当我尝试访问/ users/add页面时出现错误。

Error: Call to a member function allow() on a non-object 
File : src\Controller\UsersController.php 

出了什么问题?它不应该允许添加用户没有身份验证?

+0

您是否在'AppController'中添加了'auth'作为组件? – 2015-04-04 07:28:11

+0

本教程中的这一部分:'认证(登录和注销)'http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html – 2015-04-04 07:31:09

+0

是的存在'公共函数初始化() { $ this-> loadComponent('Flash'); $这 - > loadComponent( '验证',[ 'loginRedirect'=> [ '控制器'=> '文章', '动作'=> '索引' ], 'logoutRedirect'=> [ ' controller'=>'Pages', 'action'=>'display', 'home' ] ]); }' – 2015-04-04 07:42:24

回答

1

确保Auth Component是否正确装入您的AppController.php文件。

+0

这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 – Oldskool 2015-04-06 08:32:49

+0

@Oldskool ......如果你在作者的文章中查看了评论,你会注意到我已经放置了同样的“批评”,并且他要求我做出答案,以便他可以接受并关闭这个......这就是为什么你决定删除这个?通过删除而不检查,你阻止他接受这个答案,使他得到一个解决方案.... – 2015-04-08 13:54:14

+0

你正在回答一个问题的问题,此外没有详细说明你的答案,为什么这一步是必需的。本身没有阅读评论,这是一个不好的答案,因此我投票删除它。 – Oldskool 2015-04-08 17:59:54

1

,请务必让parent::initialize()在UsersController.php如果实现了public initialize()方法