UsersController :: beforeFilter()的声明应与AppController :: beforeFilter(Cake \ Event \ Event $ event)兼容

时间:2015-09-21 08:38:57

标签: php cakephp authentication cakephp-3.0

我对cakephp ver3.1.3没有经验

我按照说明实现了登录验证功能; http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html

我成功地用蛋糕烤了我的cakephp v3.1.3 app。

我遇到了UsersController.php

的问题

我从http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html;

复制并粘贴了以下代码
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']);
} //public function beforeFilter(Event $event)

此代码的存在会在下面创建错误;

  

严格(2048):宣言   App \ Controller \ UsersController :: beforeFilter()应兼容   使用App \ Controller \ AppController :: beforeFilter(Cake \ Event \ Event   $ event)[APP / Controller \ UsersController.php,第12行]代码上下文   include - APP / Controller \ UsersController.php,第12行   Composer \ Autoload \ includeFile - ROOT \ vendor \ composer \ ClassLoader.php,   第412行Composer \ Autoload \ ClassLoader :: loadClass() -   ROOT \ vendor \ composer \ ClassLoader.php,第301行spl_autoload_call -   [内部],线? class_exists - [内部],行?   Cake \ Core \ App :: _ classExistsInBase() - CORE \ src \ Core \ App.php,第89行   Cake \ Core \ App :: className() - CORE \ src \ Core \ App.php,第66行   Cake \ Routing \ Filter \ ControllerFactoryFilter :: _ getController() -   CORE \ src \ Routing \ Filter \ ControllerFactoryFilter.php,第81行   Cake \ Routing \ Filter \ ControllerFactoryFilter :: beforeDispatch() -   CORE \ src \ Routing \ Filter \ ControllerFactoryFilter.php,第49行   Cake \ Routing \ DispatcherFilter :: handle() -   CORE \ src \ Routing \ DispatcherFilter.php,第145行   Cake \ Event \ EventManager :: _ callListener() -   CORE \ src \ Event \ EventManager.php,第389行   Cake \ Event \ EventManager :: dispatch() - CORE \ src \ Event \ EventManager.php,   第355行Cake \ Routing \ Dispatcher :: dispatchEvent() -   CORE \ src \ Event \ EventDispatcherTrait.php,第78行   Cake \ Routing \ Dispatcher :: dispatch() - CORE \ src \ Routing \ Dispatcher.php,   第62行[main] - ROOT \ webroot \ index.php,第37行

如果删除有问题的代码段,则错误消失。代码段有什么问题?

2 个答案:

答案 0 :(得分:25)

解决方案是将以下行添加到UsersController.php

use Cake\Event\Event;

蛋糕烘焙版没有上述内容。

答案 1 :(得分:5)

在4.0.2草莓蛋糕上-现在-

use Cake\Event\EventInterface;

public function beforeFilter(EventInterface  $event)
        {
            parent::beforeFilter($event);
    }