Laravel 5.4 - Session store not set on request

Pete picture Pete · May 3, 2017 · Viewed 10.5k times · Source

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.

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

Answer

Marco Aurélio Deleu picture Marco Aurélio Deleu · May 4, 2017

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, ShareErrorsFromSession will use session to present your error message in the view. From that we can say that you have some problem here:

// \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.

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

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.