Laravel 5.4 - 未根据请求设置会话存储

时间:2017-05-02 23:14:14

标签: laravel session kernel dev-to-production

在我的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

3 个答案:

答案 0 :(得分:5)

让我们走出错误:

  

会话存储未按要求设置。

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

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

StartSession Middleware负责在Request中设置Session对象。如果您将其停用,则无法ShareErrorsFromSession

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

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

答案 1 :(得分:0)

您是否检查了storage文件夹的权限?

如果没有尝试:

chmod -R 777 storage/

答案 2 :(得分:0)

另一个原因:

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

例如,如果您将Vagrant与指向主机一起使用,而不是像127.0.0.1这样的默认主机,则会发生这种情况。