Laravel 5.4 - 未按要求设置会话存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43748652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:54:13  来源:igfitidea点击:

Laravel 5.4 - Session store not set on request

laravelsessionkerneldev-to-production

提问by Pete

On my Laravel 5.4 production server, I forgot to run the php artisan config:cache command after I implemented a multi authorization system. It all works in my development environment, however it does not want to in production. Since running the cache command, I've worked through all the errors to this point. However I am stuck on this one and really not sure where to take it. Any information I haven't provided, please feel free to ask and I will post it. Thank you so much for your help.

在我的 Laravel 5.4 生产服务器上,我在实现多授权系统后忘记运行 php artisan config:cache 命令。这一切都适用于我的开发环境,但它不想在生产环境中使用。自从运行缓存命令以来,我已经解决了所有错误到这一点。然而,我被困在这个问题上,真的不知道该把它带到哪里。任何我没有提供的信息,请随时询问,我会发布。非常感谢你的帮助。

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

回答by Marco Aurélio Deleu

Let's walk from the error:

让我们从错误中走出来:

Session store not set on request.

未按要求设置会话存储。

This means the Request object doesn't have a Session object associated with it. However, ShareErrorsFromSessionwill use session to present your error message in the view. From that we can say that you have some problem here:

这意味着 Request 对象没有与之关联的 Session 对象。但是,ShareErrorsFromSession将使用会话在视图中显示您的错误消息。由此我们可以说您在这里遇到了一些问题:

// \App\Http\Middleware\StartSession::class,

StartSession Middleware is the one responsible for setting a Session object in your Request. If you disable it, you cannot ShareErrorsFromSession.

StartSession 中间件负责在您的请求中设置会话对象。如果禁用它,则不能ShareErrorsFromSession

But just uncommenting that might not be enough if you don't have a valid SESSION_DRIVERthat will tell StartSessionmiddleware which Session Driver it should use. So lastly, check your .envfile and make sure to use a valid Session Driver.

但是,如果您没有一个有效的SESSION_DRIVER来告诉StartSession中间件它应该使用哪个会话驱动程序,那么仅仅取消注释可能还不够。所以最后,检查您的.env文件并确保使用有效的会话驱动程序。

All this is to complement Mathieu's answer: Your web server needs writing permissioon on storage/folder. Easiest way to achieve that is with chmod 777. For more on it, if you understand how scripts are invoked and knows which user is controlling Nginx, you can prepare your files to be owned by that user and set it to 755.

所有这些都是为了补充 Mathieu 的回答:您的 Web 服务器需要在storage/文件夹上写入权限。实现这一目标的最简单方法是使用chmod 777. 有关更多信息,如果您了解脚本的调用方式并知道哪个用户正在控制 Nginx,则可以准备文件以供该用户拥有并将其设置为755.

回答by Mathieu Ferre

Did you check the permissions on the storagefolder ?

你检查过storage文件夹的权限了吗?

if not try :

如果不尝试:

chmod -R 777 storage/

回答by Amirreza Nasiri

Just another reason:

只是另一个原因:

If you're using Laravel Sanctum(or somehow adding EnsureFrontendRequestsAreStateful) and the session is not being started, you may need to add your production and development URL of the project to the SANCTUM_STATEFUL_DOMAINSenvironment variable.

如果您正在使用Laravel Sanctum(或以某种方式添加EnsureFrontendRequestsAreStateful)并且会话没有启动,您可能需要将项目的生产和开发 URL 添加到SANCTUM_STATEFUL_DOMAINS环境变量中。

It happens if you're for example using Vagrant with a pointed host rather than default ones like 127.0.0.1.

例如,如果您使用带有尖头主机的 Vagrant 而不是像 127.0.0.1 这样的默认主机,就会发生这种情况。