Asked  9 Months ago    Answers:  5   Viewed   382 times

I'm trying to share a Session value with all views using AppServiceProvider class.

In the boot() function I said: view()->share('key', Session::get('key'));

But, the value is null. What can be the problem? In the Controller I'm setting it, it works fine. Even after removing the Session::put() line, the value is still in the session (obviously).

 Answers

70

In Laravel session is initialized in a middleware that is handled by this class:

IlluminateSessionMiddlewareStartSession::class

When the service providers are booted, this middleware has not been executed, because all the middlewares execute after the service providers boot phase

So, instead of sharing the variable from a service provider, you can create a middleware and share the session variable from there, or you can use a view composer with a callback in your service provider:

public function boot()
{
    view()->composer('*', function ($view) 
    {
        //this code will be executed when the view is composed, so session will be available
        $view->with('key', Session::get('key') );    
    });  
}

This will work, as the callback will be called before the views are composed, when the middleware has already been executed, so session will be availabe

In general, pay attention at your middleware's execution order: if you want to access the session from a middleware, that should execute after Laravel's StartSession::class middleware

Wednesday, March 31, 2021
 
christina
answered 9 Months ago
49

You can't read session directly from a service provider: in Laravel the session is handled by StartSession middleware that executes after all the service providers boot phase

If you want to share a session variable with all view, you can use a view composer from your service provider:

public function boot()
{
    view()->composer('*', function ($view) 
    {
        $view->with('your_var', Session::get('var') );    
    });  
}

The callback passed as the second argument to the composer will be called when the view will be rendered, so the StartSession will be already executed at that point

Wednesday, March 31, 2021
 
aaronhuisinga
answered 9 Months ago
87

While your users are accessing via HTTPS, your CloudFlare configuration is passing those requests onto your server using HTTP. You have two options:

  1. Turn on CloudFlare's Full or Strict SSL. https://blog.cloudflare.com/origin-server-connection-security-with-universal-ssl/

  2. Set up your webserver to see the X-Forwarded-Proto header and tell PHP it's being accessed via HTTPS when that header's value is https. How to do this depends a bit on your webserver. In nginx, for example, I do this for Amazon's ELB (which sends the same headers):

map $http_x_forwarded_proto $is_https {
    default off;
    https on;
}

and then this in my FastCGI params file:

fastcgi_param  HTTPS              $is_https if_not_empty;

In Apache, I think you can do this if mod_setenvif is installed:

SetEnvIf x-forwarded-proto https HTTPS=on
Wednesday, March 31, 2021
 
supermitch
answered 9 Months ago
69

1. You could use where() closure:

User::where(function ($q) use($city) {
    if ($city) {
        $q->where('city', $city);
    }
})->orderBy('created_at')->paginate(10);

2. You could use when():

User::when($city, function ($q) use ($city) {
    return $q->where('city', $city);
})->orderBy('created_at')->paginate(10);

3. Do this:

$users = User::orderBy('created_at');

if ($city) {
    $users = $users->where('city', $city);
}

$users = $users->paginate(10);
Saturday, May 29, 2021
 
Gregosaurus
answered 7 Months ago
73

Try wrapping your routes (inside app/Http/routes.php) in a Route::group() with the web middleware:

Route::group(['middleware' => ['web']], function () {
    // My Routes
});

An easy way to test this:

Route::group(['middleware' => 'web'], function () {
    Route::get('', function () {
        Session::set('test', 'testing');
    });

    Route::get('other', function () {
        dd(Session::get('test'));
    });
});

If you remove the web middleware, you'll receive null since the web middleware is responsible for starting the session.

Ensure you have the web middleware group inside your app/Http/Kernel.php:

protected $middlewareGroups = [
    'web' => [
        MiddlewareEncryptCookies::class,
        IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
        IlluminateSessionMiddlewareStartSession::class,
        IlluminateViewMiddlewareShareErrorsFromSession::class,
        MiddlewareVerifyCsrfToken::class,
    ],
];
Saturday, May 29, 2021
 
exxed
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share