laravel11中如何关闭csrf验证?
最近使用laravel11开发新项目,发下很多跟之前不太一样的,单独开贴记录下来。以方便别人通过搜索引擎搜索解决。
laravel11 里面关闭csrf 验证的方法
- 找到
bootstrap/app.php
文件 - 设置
$middleware->validateCsrfTokens(['*'])
完整代码如下
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->validateCsrfTokens([
'*'
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
本作品采用《CC 协议》,转载必须注明作者和本文链接
全关的话直接从
Kernel.php
里把VerifyCsrfToken
中间件注释了就行了,关部分的话,在VerifyCsrfToken
的$except
属性里排除路由。你看这个写法你喜欢么?
支持下,大佬太好了