Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Authentication cakephp 4-烘焙新控制器时未找到身份验证组件_Authentication_Cakephp_Cakephp Bake_Cakephp 4.x - Fatal编程技术网

Authentication cakephp 4-烘焙新控制器时未找到身份验证组件

Authentication cakephp 4-烘焙新控制器时未找到身份验证组件,authentication,cakephp,cakephp-bake,cakephp-4.x,Authentication,Cakephp,Cakephp Bake,Cakephp 4.x,我无法使用cake bake,我有一个与身份验证组件相关的错误。 应用程序使用此组件。 我错过了什么 以下是完整日志: bin/cake bake controller styles 1 ↵ Baking controller class for Styles... Creating file /opt/applications/p35/src/Controller/StylesController.php Wro

我无法使用cake bake,我有一个与身份验证组件相关的错误。 应用程序使用此组件。 我错过了什么

以下是完整日志:

bin/cake bake controller styles                                         1 ↵
Baking controller class for Styles...

Creating file /opt/applications/p35/src/Controller/StylesController.php
Wrote `/opt/applications/p35/src/Controller/StylesController.php`
Bake is detecting possible fixtures...
Exception: The request object does not contain the required `authentication` attribute
In [/opt/applications/p35/vendor/cakephp/authentication/src/Controller/Component/AuthenticationComponent.php, line 141]

2020-03-13 18:20:25 Error: [Exception] The request object does not contain the required `authentication` attribute in /opt/applications/p35/vendor/cakephp/authentication/src/Controller/Component/AuthenticationComponent.php on line 141
Stack Trace:
- /opt/applications/p35/vendor/cakephp/authentication/src/Controller/Component/AuthenticationComponent.php:229
- /opt/applications/p35/src/Controller/AppController.php:82
- /opt/applications/p35/vendor/cakephp/cakephp/src/Controller/Controller.php:212
- /opt/applications/p35/vendor/cakephp/bake/src/Command/TestCommand.php:339
- /opt/applications/p35/vendor/cakephp/bake/src/Command/TestCommand.php:245
- /opt/applications/p35/vendor/cakephp/bake/src/Command/TestCommand.php:120
- /opt/applications/p35/vendor/cakephp/bake/src/Command/ControllerCommand.php:201
- /opt/applications/p35/vendor/cakephp/bake/src/Command/ControllerCommand.php:147
- /opt/applications/p35/vendor/cakephp/bake/src/Command/ControllerCommand.php:64
- /opt/applications/p35/vendor/cakephp/cakephp/src/Console/BaseCommand.php:175
- /opt/applications/p35/vendor/cakephp/cakephp/src/Console/CommandRunner.php:336
- /opt/applications/p35/vendor/cakephp/cakephp/src/Console/CommandRunner.php:171
- /opt/applications/p35/bin/cake.php:12
谢谢

以及公共函数initialize()中AppController.php的第82行


烘焙控制器测试时,烘焙将创建相关控制器类的实例,以便检索该控制器的默认模型,它使用该模型绘制相关夹具,该夹具将添加到生成的控制器测试用例的
$fixtures
属性中

当实例化一个控制器时,它的
initialize()
方法将在构造时被调用,这就是问题的开始。您正在控制器
initialize()
方法中调用
$this->Authentication->getResult()
,导致身份验证组件在附加到控制器的请求对象中查找所需的身份验证数据,并且确信它不会找到任何此类数据,因为没有进行身份验证

您应该将调用身份验证组件的逻辑移到controllers
beforeFilter()
回调方法中,默认情况下,此方法仅在controllerfactory或异常呈现程序调用controllers
startupProcess()
方法时运行,这通常是在实际的HTTP请求上下文中,或者在可以提供可能需要的身份验证数据的单元测试上下文中

另见


您使用的认证插件的确切版本是什么?如果您不确定,请在您的
composer.lock
文件中查找
“cakephp/authentication”
。还有
AppController
中的line
82
到底做了什么,它位于什么上下文中?“name:“cakephp/authentication”,“version:“2.1.0”,grep authentication composer.json“cakephp/authentication”:“^2.0”,我已经用第82行更新了初始帖子。从父级调用它的位置来看,我假设代码在controllers
initialize()
方法中?感谢您的解释,这非常清楚,解决了我的问题。
76    $this->loadComponent('Authentication.Authentication',[
77              'requireIdentity'=>true,
78              'logoutRedirect' => '/users/login']);
79
80
81    $this->user=$this->Authentication->getIdentity();
82        $result = $this->Authentication->getResult();
83        if ($result->isValid()) {
84          $identity=$this->Authentication->getIdentity();
85
            ...