laravel 5.5 session不是在web中间件中启动的

时间:2018-04-03 01:39:02

标签: php laravel laravel-5.5

这是我在使用dd(session())时获得的数组。

SessionManager {#551 ▼
  #app: Application {#5 ▶}
  #customCreators: []
  #drivers: array:1 [▼
    "file" => Store {#553 ▼
      #id: "Kg81ge6ve7bPg4Vos5qsKqvU3Bmo2k6U0hQncxY1"
      #name: "project_session"
      #attributes: array:1 [▶]
      #handler: FileSessionHandler {#552 ▶}
      #started: false
    }
  ]
}

使用auth中间件工作正常。

SessionManager {#739 ▼
  #app: Application {#5 ▶}
  #customCreators: []
  #drivers: array:1 [▼
    "file" => Store {#742 ▼
      #id: "3nTG8ApjScmeivyKtwDw1nsfOQ9hK8lrwkbLJfTa"
      #name: "project_session"
      #attributes: array:5 [▶]
      #handler: FileSessionHandler {#741 ▶}
      #started: true
    }
  ]
}

我正在建立网站,客户登录。我试图通过用户登录时设置的会话来访问货币。但是当我尝试在会话中设置它然后它没有设置。 当我查看会话文件夹时,每个新请求都有新文件。

希望有人经历过这个。因为我经常搜索,但没有找到任何东西。提前谢谢。

这是我的kernel.php

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
    ];

    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];


    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    ];
}

这是我的routes.php

<?php
Route::group(['middleware' => ['web']], function(){
    Route::get('/', 'Front\HomeController@index');
    Route::get('/blog','Front\BlogController@index');
    Route::get('/blog/{slug}','Front\BlogController@show');
    Route::get('/{slug}','Front\PageController@show');
});

//Routes For Customer panel
Auth::routes();

Route::get('/customer/home', 'Customer\HomeController@index')->name('home');

Route::group( ['middleware' => ['auth'], 'prefix' => 'customer'], function(){
    Route::resource('products','Customer\ProductController');
    Route::resource('categories','Customer\CategoryController');
    Route::resource('posts','Customer\PostController');
    Route::resource('pages','Customer\PageController');
    Route::resource('packages','Customer\PackageController');
    Route::resource('clients','Customer\ClientController');
    Route::resource('faqs','Customer\FaqController');
    Route::resource('testimonials','Customer\TestimonialController');
    Route::resource('blogs','Customer\BlogController');
    Route::resource('currencies','Customer\CurrencyController');
});

我尝试从路由中删除Web中间件,因为有人建议自动为路由设置Web中间件。因此,如果您添加Web中间件,那么您可以将路由作为“web,web”中间件,但是当我使用php artisan route获取路由时:list它只给我网络中间件。

我不知道会话开始是如何工作的,有什么方法我可以添加到某个地方,所以我可以开始会话。

1 个答案:

答案 0 :(得分:0)

因为您的会话中间件插入到Web组中间件中,所以它只在Web组中间件中运行。 你应该替换它们并像这样插入中间件:

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    \App\Http\Middleware\TrustProxies::class,
    **\Illuminate\Session\Middleware\StartSession::class,// insert here 
    \Illuminate\View\Middleware\ShareErrorsFromSession::class // insert here**
];

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        **\Illuminate\Session\Middleware\StartSession::class,// remove this**
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        **\Illuminate\View\Middleware\ShareErrorsFromSession::class // remove this**
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];


protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];