2015-04-04 34 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()方法