Laravel 5.4 - 未按要求设置会话存储
Laravel 5.4 - Session store not set on request
在我的 Laravel 5.4 生产服务器上,我在实施多授权系统后忘记了 运行 php artisan config:cache 命令。这一切都适用于我的开发环境,但它不想在生产环境中使用。自从 运行 执行缓存命令以来,到目前为止,我已经解决了所有错误。但是我被困在这个上,真的不知道该把它带到哪里。任何我没有提供的信息,请随时询问,我会 post 提供。非常感谢您的帮助。
namespace App\Exceptions;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
];
public function report(Exception $exception)
{
parent::report($exception);
}
public function render($request, Exception $exception)
{
return parent::render($request, $exception);
}
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
$guard=array_get($exception->guards(),0);
switch ($guard) {
case 'admin':
$login='admin.login';
break;
default:
$login='login';
break;
}
return redirect()->guest(route($login));
}
}
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
public function handle($request, Closure $next, $guard = null)
{
switch ($guard) {
case 'admin':
if (Auth::guard($guard)->check()) {
return redirect()->route('admin');
}
break;
default:
if (Auth::guard($guard)->check()) {
return redirect('/user');
}
break;
}
return $next($request);
}
}
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,
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
// \App\Http\Middleware\StartSession::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,
];
}
RuntimeException in Request.php line 388:Session store not set on request.
1. in Request.php line 388
2. at Request->session() in ShareErrorsFromSession.php line 42
3. at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
4. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
5. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
6. at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
7. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
8. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
9. at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
10. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
11. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
12. at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
13. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
14. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
15. at Pipeline->then(object(Closure)) in Router.php line 561
16. at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
17. at Router->dispatchToRoute(object(Request)) in Router.php line 498
18. at Router->dispatch(object(Request)) in Kernel.php line 174
19. at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
20. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
21. at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
22. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
23. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
24. at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
25. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
26. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
27. at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
28. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
29. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
30. at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
31. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
32. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
33. at Pipeline->then(object(Closure)) in Kernel.php line 149
34. at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
35. at Kernel->handle(object(Request)) in index.php line 53
您是否检查了 storage
文件夹的权限?
如果不尝试:
chmod -R 777 storage/
让我们从错误中走出来:
Session store not set on request.
这意味着 Request 对象没有关联的 Session 对象。但是,ShareErrorsFromSession
将使用会话在视图中显示您的错误消息。由此我们可以说您在这里遇到了一些问题:
// \App\Http\Middleware\StartSession::class,
StartSession 中间件负责在您的请求中设置 Session 对象。如果禁用它,则不能 ShareErrorsFromSession
.
但是如果您没有有效的 SESSION_DRIVER
会告诉 StartSession
中间件它应该使用哪个会话驱动程序,那么仅仅取消注释可能还不够。所以最后,检查您的 .env
文件并确保使用有效的会话驱动程序。
所有这些都是为了补充 Mathieu 的回答:您的网络服务器需要对 storage/
文件夹的写入权限。最简单的方法是使用 chmod 777
。关于它的更多信息,如果您了解脚本是如何被调用的并且知道哪个用户正在控制 Nginx,您可以准备您的文件由该用户拥有并将其设置为 755
.
还有一个原因:
如果您正在使用 Laravel Sanctum(或以某种方式添加 EnsureFrontendRequestsAreStateful
)并且会话未启动,您可能需要添加项目的生产和开发 URL到 SANCTUM_STATEFUL_DOMAINS
环境变量。
例如,如果您将 Vagrant 与指定主机一起使用,而不是像 127.0.0.1 这样的默认主机,就会发生这种情况。
在我的 Laravel 5.4 生产服务器上,我在实施多授权系统后忘记了 运行 php artisan config:cache 命令。这一切都适用于我的开发环境,但它不想在生产环境中使用。自从 运行 执行缓存命令以来,到目前为止,我已经解决了所有错误。但是我被困在这个上,真的不知道该把它带到哪里。任何我没有提供的信息,请随时询问,我会 post 提供。非常感谢您的帮助。
namespace App\Exceptions;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
];
public function report(Exception $exception)
{
parent::report($exception);
}
public function render($request, Exception $exception)
{
return parent::render($request, $exception);
}
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
$guard=array_get($exception->guards(),0);
switch ($guard) {
case 'admin':
$login='admin.login';
break;
default:
$login='login';
break;
}
return redirect()->guest(route($login));
}
}
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
public function handle($request, Closure $next, $guard = null)
{
switch ($guard) {
case 'admin':
if (Auth::guard($guard)->check()) {
return redirect()->route('admin');
}
break;
default:
if (Auth::guard($guard)->check()) {
return redirect('/user');
}
break;
}
return $next($request);
}
}
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,
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
// \App\Http\Middleware\StartSession::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,
];
}
RuntimeException in Request.php line 388:Session store not set on request.
1. in Request.php line 388
2. at Request->session() in ShareErrorsFromSession.php line 42
3. at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
4. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
5. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
6. at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
7. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
8. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
9. at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
10. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
11. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
12. at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
13. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
14. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
15. at Pipeline->then(object(Closure)) in Router.php line 561
16. at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
17. at Router->dispatchToRoute(object(Request)) in Router.php line 498
18. at Router->dispatch(object(Request)) in Kernel.php line 174
19. at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
20. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
21. at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
22. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
23. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
24. at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
25. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
26. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
27. at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
28. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
29. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
30. at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
31. at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
32. at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
33. at Pipeline->then(object(Closure)) in Kernel.php line 149
34. at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
35. at Kernel->handle(object(Request)) in index.php line 53
您是否检查了 storage
文件夹的权限?
如果不尝试:
chmod -R 777 storage/
让我们从错误中走出来:
Session store not set on request.
这意味着 Request 对象没有关联的 Session 对象。但是,ShareErrorsFromSession
将使用会话在视图中显示您的错误消息。由此我们可以说您在这里遇到了一些问题:
// \App\Http\Middleware\StartSession::class,
StartSession 中间件负责在您的请求中设置 Session 对象。如果禁用它,则不能 ShareErrorsFromSession
.
但是如果您没有有效的 SESSION_DRIVER
会告诉 StartSession
中间件它应该使用哪个会话驱动程序,那么仅仅取消注释可能还不够。所以最后,检查您的 .env
文件并确保使用有效的会话驱动程序。
所有这些都是为了补充 Mathieu 的回答:您的网络服务器需要对 storage/
文件夹的写入权限。最简单的方法是使用 chmod 777
。关于它的更多信息,如果您了解脚本是如何被调用的并且知道哪个用户正在控制 Nginx,您可以准备您的文件由该用户拥有并将其设置为 755
.
还有一个原因:
如果您正在使用 Laravel Sanctum(或以某种方式添加 EnsureFrontendRequestsAreStateful
)并且会话未启动,您可能需要添加项目的生产和开发 URL到 SANCTUM_STATEFUL_DOMAINS
环境变量。
例如,如果您将 Vagrant 与指定主机一起使用,而不是像 127.0.0.1 这样的默认主机,就会发生这种情况。