会话存储未在Laravel 5.3中按请求设置

时间:2017-07-07 04:31:48

标签: php laravel laravel-5.3

我的网站出现了这样的错误:

enter image description here

我还尝试了一些解决方案,比如在中间件网络中包裹路由它没有工作

这是我的Kernel.php

<?php

namespace App\Http;

use App\Http\Middleware\Activated;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    ];

    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \anlutro\LaravelSettings\SaveMiddleware::class,
            \Barryvdh\Cors\HandleCors::class,
            Activated::class
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];
(The rest of file)
    }

我如何解决这个问题:((

3 个答案:

答案 0 :(得分:3)

如果您需要会话状态,CSRF保护等,则需要使用Web中间件。

Route::group(['middleware' => ['web']], function () {
    // your routes here
});

你的kernel.php看起来应该是这样的

protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Session\Middleware\StartSession::class,
    ];

    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,

            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \anlutro\LaravelSettings\SaveMiddleware::class,
            \Barryvdh\Cors\HandleCors::class,
            Activated::class
        ],

答案 1 :(得分:1)

如果在网络中间件中添加路径因任何原因无效,请尝试将此 $ middleware 添加到<强> Kernel.php

protected $middleware = [
    //...
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
];

答案 2 :(得分:0)

您好需要创建中间件

php artisan make:middleware AnyNameYouWant

它将为您创建一个漂亮的样板。

public function handle($request, Closure $next)
{
   // Your Logic
}

  return redirect('whereever you want to redirect');
}

然后将其包含在您的中间件

   'web' => [
        \App\Http\Middleware\EncryptCookies::class,

      \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \anlutro\LaravelSettings\SaveMiddleware::class,
        \Barryvdh\Cors\HandleCors::class,
        \App\Http\Middleware\YourMiddleware::class
    ],    

我希望这会有所帮助