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

标签 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 应用程序。

我对 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)

此代码的存在导致以下错误;

Strict (2048): Declaration of App\Controller\UsersController::beforeFilter() should be compatible with App\Controller\AppController::beforeFilter(Cake\Event\Event $event) [APP/Controller\UsersController.php, line 12] Code Context include - APP/Controller\UsersController.php, line 12 Composer\Autoload\includeFile - ROOT\vendor\composer\ClassLoader.php, line 412 Composer\Autoload\ClassLoader::loadClass() - ROOT\vendor\composer\ClassLoader.php, line 301 spl_autoload_call - [internal], line ?? class_exists - [internal], line ?? Cake\Core\App::_classExistsInBase() - CORE\src\Core\App.php, line 89 Cake\Core\App::className() - CORE\src\Core\App.php, line 66 Cake\Routing\Filter\ControllerFactoryFilter::_getController() - CORE\src\Routing\Filter\ControllerFactoryFilter.php, line 81 Cake\Routing\Filter\ControllerFactoryFilter::beforeDispatch() - CORE\src\Routing\Filter\ControllerFactoryFilter.php, line 49 Cake\Routing\DispatcherFilter::handle() - CORE\src\Routing\DispatcherFilter.php, line 145 Cake\Event\EventManager::_callListener() - CORE\src\Event\EventManager.php, line 389 Cake\Event\EventManager::dispatch() - CORE\src\Event\EventManager.php, line 355 Cake\Routing\Dispatcher::dispatchEvent() - CORE\src\Event\EventDispatcherTrait.php, line 78 Cake\Routing\Dispatcher::dispatch() - CORE\src\Routing\Dispatcher.php, line 62 [main] - ROOT\webroot\index.php, line 37

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

最佳答案

解决方案是在 UsersController.php 中添加以下行

use Cake\Event\Event;

蛋糕烘焙版没有上面这行

关于php - UsersController::beforeFilter() 的声明应该与 AppController::beforeFilter(Cake\Event\Event $event) 兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32690756/

相关文章:

cakephp - 如何测试用户是否在 Cakephp 2.0 auth 组件中处于事件状态?

java - 在 Java 应用程序中创建 session

authentication - Cypress 插入已验证的 token

php - 列计数与插入表的第1行的值计数不匹配

php - 从AJAX发送到php文件时如何区分表单数据和jQuery变量?

mysql - CakePHP 虚拟字段 : replace one string with another

authentication - Haskell 独立桌面应用程序身份验证(使用 google/facebook/twitter/etc 帐户)

php - 如何忽略 PHP 中准备好的 mysqli 查询中的参数?

javascript - 在共享主机上托管 laravel 5 应用程序 : getting js error "tinymce is not defined"

php - CakePHP 分页 - 如何从 url 中删除 "page:"以获得更好的 seo/cleaner URL