我有以下代码块:
return redirect()->action('mycontroller@myfunction',
[
'data0' => $request->data0,
'data1' => $request->data1,
'data2' => $request->data2,
'data3' => $request->data3,
]);
显示错误:MethodNotAllowedHttpException 可能是因为 csrf token 未通过此路由传递,该路由是后路由。
Route::post('congrats', 'mycontroller@myfunction');
我不希望 data0、data1 等值显示在 url 中,所以这就是我希望它成为 POST 的原因。
如何在重定向操作中传递 csrf token ?还有其他方法吗?例如使用 GET 但隐藏特定数据?
最佳答案
像这样编辑您的 VerifyCsrfToken
类以禁用对路由的验证
protected $except_urls = [
'contact/create',
'contact/update',
...
];
public function handle($request, Closure $next)
{
$regex = '#' . implode('|', $this->except_urls) . '#';
if ($this->isReading($request) || $this->tokensMatch($request) || preg_match($regex, $request->path()))
{
return $this->addCookieToResponse($request, $next($request));
}
throw new TokenMismatchException;
}
并更改内核以指向新的中间件:
protected $middleware = [
...
'App\Http\Middleware\VerifyCsrfToken',
];
编辑
在 Laravel 文档中
将其添加到 VerifyCsrfToken
类
protected $except = [
'stripe/*',
];
关于laravel - 如何添加csrf token id重定向()->操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46794285/