我正在尝试构建一个 api,出于某种原因,我需要会话.但是如果我包含 web 中间件,我会收到 CSRF 错误,如果我不包含,我就无法启动 session.
Im trying to build an api, and for some reason I need sessions. But if I include web middleware I get CSRF errors, and if I dont I cant have session started.
如何解决这个问题?
转到 app/Http/Kernel.php 并将您自己的名称(如sessions")添加到 $middlewareGroups.它应该包含IlluminateSessionMiddlewareStartSession::class,
go to app/Http/Kernel.php and add your own name like 'sessions' to the $middlewareGroups. It should contain IlluminateSessionMiddlewareStartSession::class,
为您想要的路线分配会话".
Assign 'sessions' to those routes you want.
protected $middlewareGroups = [
'web' => [
AppHttpMiddlewareEncryptCookies::class,
IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
IlluminateSessionMiddlewareStartSession::class,
IlluminateViewMiddlewareShareErrorsFromSession::class,
AppHttpMiddlewareVerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
'sessions' => [
IlluminateSessionMiddlewareStartSession::class,
]
];
路由/api.php
Route::group(['middleware' => ['sessions']], function () {
Route::resource(...);
});
这篇关于Laravel 5.3 - 如何在没有 CSRF 的情况下将会话添加到`API`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Laravel Eloquent Union 查询Laravel Eloquent Union query(Laravel Eloquent Union 查询)
覆盖 Laravel 5 辅助函数Overwrite laravel 5 helper function(覆盖 Laravel 5 辅助函数)
laravel querybuilder 如何在 where 函数中使用 likelaravel querybuilder how to use like in wherein function(laravel querybuilder 如何在 where 函数中使用 like)
响应内容必须是实现 __toString()、“boolean"和“The Response content must be a string or object implementing __toString(), quot;booleanquot; given after move to psql(响应内容必须是实现 __toS
Laravel 5 的角色,如何只允许管理员访问某些根Roles with laravel 5, how to allow only admin access to some root(Laravel 5 的角色,如何只允许管理员访问某些根)
Laravel Auth - 使用 md5 而不是集成的 Hash::make()Laravel Auth - use md5 instead of the integrated Hash::make()(Laravel Auth - 使用 md5 而不是集成的 Hash::make())