Laravel 5.3 - 如何在没有 CSRF 的情况下将会话添加到`API`?

Laravel 5.3 - How to add Sessions to `API` without CSRF?(Laravel 5.3 - 如何在没有 CSRF 的情况下将会话添加到`API`?)
本文介绍了Laravel 5.3 - 如何在没有 CSRF 的情况下将会话添加到`API`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试构建一个 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`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)