在 CakePHP 4 的请求数据中找不到 _Token
_Token was not found in request data in CakePHP 4
我对 cakephp 4 中的简单注册功能有一些疑问,
我启用了 FormProtection 组件,它 return"
_在请求数据中找不到令牌。”
token已经生成了,不知道为什么controller检测不到
<?= $this->Form->create($user,['class' => '']) ?>
<?= $this->Flash->render('signup') ?>
<div class="inp-row">
<label for="">Full Name</label>
<?= $this->Form->input('name',["placeholder"=>"Your full name",'label'=>false,'required'=>true,'class' => '']) ?>
</div>
<div class="inp-row">
<label for="">Email Address</label>
<?= $this->Form->input('username',["placeholder"=>"Your email address",'label'=>false,'required'=>true,'type'=>'email','class' => '']) ?>
</div>
<div class="inp-row">
<label for="">Password</label>
<?= $this->Form->input('password',["placeholder"=>"Type your password",'label'=>false,'id'=>"pass_change" ,'required'=>true,'type'=>'password','class' => '']) ?>
</div>
<?= $this->Form->button(__('Sign up'));?>
<?= $this->Form->end() ?>
函数注册
$user = $this->Users->newEmptyEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__("Your account has been created successfully."), [ 'key' => 'signup']);
}
}
$this->set('user', $user);
2020-05-05 23:48:32 Error: [Cake\Http\Exception\BadRequestException] `_Token` was not found in request data. in xxxxxx/vendor/cakephp/cakephp/src/Controller/Component/FormProtectionComponent.php on line 141
Stack Trace:
- xxxxxx/vendor/cakephp/cakephp/src/Controller/Component/FormProtectionComponent.php:95
- xxxxxx/vendor/cakephp/cakephp/src/Event/EventManager.php:309
- xxxxxx/vendor/cakephp/cakephp/src/Event/EventManager.php:286
- xxxxxx/vendor/cakephp/cakephp/src/Event/EventDispatcherTrait.php:92
- xxxxxx/vendor/cakephp/cakephp/src/Controller/Controller.php:569
- xxxxxx/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php:72
- xxxxxx/vendor/cakephp/cakephp/src/Http/BaseApplication.php:229
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:77
- xxxxxx/vendor/cakephp/cakephp/src/Http/Middleware/BodyParserMiddleware.php:164
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:73
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:77
- xxxxxx/vendor/cakephp/cakephp/src/Http/Middleware/CsrfProtectionMiddleware.php:137
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:73
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:58
- xxxxxx/vendor/cakephp/cakephp/src/Routing/Middleware/RoutingMiddleware.php:162
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:73
- xxxxxx/vendor/cakephp/cakephp/src/Routing/Middleware/AssetMiddleware.php:68
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:73
- xxxxxx/vendor/cakephp/cakephp/src/Error/Middleware/ErrorHandlerMiddleware.php:119
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:73
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:58
- xxxxxx/vendor/cakephp/cakephp/src/Http/Server.php:90
- xxxxxx/webroot/index.php:40
安全组件已弃用,其功能已提取到表单保护组件和 HTTPS 执行器中间件中。它已被弃用的事实可能应该在 Cookbook 中更突出地呈现。
同时使用表单保护组件和安全组件将导致您遇到的错误,因为安全组件和表单保护组件都会从中删除 _Token
请求数据一旦被验证,因此其中之一会抱怨令牌不存在。
长话短说,不要使用安全组件,只使用表单保护组件。如果您使用的是安全组件的 require SSL 功能,请改用最初提到的 HTTPS 执行器中间件。
另见
我对 cakephp 4 中的简单注册功能有一些疑问, 我启用了 FormProtection 组件,它 return" _在请求数据中找不到令牌。”
token已经生成了,不知道为什么controller检测不到
<?= $this->Form->create($user,['class' => '']) ?>
<?= $this->Flash->render('signup') ?>
<div class="inp-row">
<label for="">Full Name</label>
<?= $this->Form->input('name',["placeholder"=>"Your full name",'label'=>false,'required'=>true,'class' => '']) ?>
</div>
<div class="inp-row">
<label for="">Email Address</label>
<?= $this->Form->input('username',["placeholder"=>"Your email address",'label'=>false,'required'=>true,'type'=>'email','class' => '']) ?>
</div>
<div class="inp-row">
<label for="">Password</label>
<?= $this->Form->input('password',["placeholder"=>"Type your password",'label'=>false,'id'=>"pass_change" ,'required'=>true,'type'=>'password','class' => '']) ?>
</div>
<?= $this->Form->button(__('Sign up'));?>
<?= $this->Form->end() ?>
函数注册
$user = $this->Users->newEmptyEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__("Your account has been created successfully."), [ 'key' => 'signup']);
}
}
$this->set('user', $user);
2020-05-05 23:48:32 Error: [Cake\Http\Exception\BadRequestException] `_Token` was not found in request data. in xxxxxx/vendor/cakephp/cakephp/src/Controller/Component/FormProtectionComponent.php on line 141
Stack Trace:
- xxxxxx/vendor/cakephp/cakephp/src/Controller/Component/FormProtectionComponent.php:95
- xxxxxx/vendor/cakephp/cakephp/src/Event/EventManager.php:309
- xxxxxx/vendor/cakephp/cakephp/src/Event/EventManager.php:286
- xxxxxx/vendor/cakephp/cakephp/src/Event/EventDispatcherTrait.php:92
- xxxxxx/vendor/cakephp/cakephp/src/Controller/Controller.php:569
- xxxxxx/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php:72
- xxxxxx/vendor/cakephp/cakephp/src/Http/BaseApplication.php:229
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:77
- xxxxxx/vendor/cakephp/cakephp/src/Http/Middleware/BodyParserMiddleware.php:164
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:73
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:77
- xxxxxx/vendor/cakephp/cakephp/src/Http/Middleware/CsrfProtectionMiddleware.php:137
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:73
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:58
- xxxxxx/vendor/cakephp/cakephp/src/Routing/Middleware/RoutingMiddleware.php:162
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:73
- xxxxxx/vendor/cakephp/cakephp/src/Routing/Middleware/AssetMiddleware.php:68
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:73
- xxxxxx/vendor/cakephp/cakephp/src/Error/Middleware/ErrorHandlerMiddleware.php:119
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:73
- xxxxxx/vendor/cakephp/cakephp/src/Http/Runner.php:58
- xxxxxx/vendor/cakephp/cakephp/src/Http/Server.php:90
- xxxxxx/webroot/index.php:40
安全组件已弃用,其功能已提取到表单保护组件和 HTTPS 执行器中间件中。它已被弃用的事实可能应该在 Cookbook 中更突出地呈现。
同时使用表单保护组件和安全组件将导致您遇到的错误,因为安全组件和表单保护组件都会从中删除 _Token
请求数据一旦被验证,因此其中之一会抱怨令牌不存在。
长话短说,不要使用安全组件,只使用表单保护组件。如果您使用的是安全组件的 require SSL 功能,请改用最初提到的 HTTPS 执行器中间件。
另见