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

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`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)