P粉9785510812023-09-02 00:13:22
是的,可以。并且您需要执行以下操作-
namespace App\Http\Middleware; class ChangeSessionValueDynamically { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, $next) { $lifetime = 1440; /* Minutes*/ config(['session.lifetime' => $lifetime]); /* Helper function */ return $next($request); } }
在 \Illuminate\Session\Middleware\StartSession::class,
之前添加 \App\Http\Middleware\ChangeSessionValueDynamically::class,
。
/** * The priority-sorted list of middleware. * * This forces non-global middleware to always be in the given order. * * @var array */ protected $middlewarePriority = [ .... .... \App\Http\Middleware\ChangeSessionValueDynamically::class \Illuminate\Session\Middleware\StartSession::class, .... .... ];
\App\Http\Middleware\ChangeSessionValueDynamically::class
的别名,如下所示 -/** * The application's route middleware. * * These middleware may be assigned to groups or used individually. * * @var array */ protected $routeMiddleware = [ .... .... 'dynamic_session' => \App\Http\Middleware\ChangeSessionValueDynamically::class, .... .... ];
A) For Group Routes- Route::group([.., 'middleware' => ['dynamic_session', ..]], function () { .. Route::get('route_1', ['uses' => 'AbcController@index', 'as' => 'abc.index']); .. } B) OR For single Route- Route::get('route_1', ['uses' => 'AbcController@index', 'as' => 'abc.index'])->middleware('dynamic_session');