17

Is there any way available to access Session values in AppServiceProvider? I would like to share session value globally in all views.

Moppo
  • 18,797
  • 5
  • 65
  • 64
Qazi
  • 5,015
  • 8
  • 42
  • 62

3 Answers3

36

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

Moppo
  • 18,797
  • 5
  • 65
  • 64
3

Add new web middleware ShareDataForView

in \app\Http\Kernel.php:

protected $middlewareGroups = [
    'web' => [
        // ...
        \Illuminate\Session\Middleware\StartSession::class,
        // I will always ShareDataForView after StartSession
        \App\Http\Middleware\ShareDataForView::class,
...

and write your code in method "handle" of app\Http\Middleware\ShareDataForView.php, for example:

<?php

namespace App\Http\Middleware;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Closure;
use Log, Exception, View;

class ShareDataForView
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        $user  = Auth::user();
        $bank = NULL;
        if ( $user ){
            $bank = $user->bank;
        }
        View::share('user', $user);
        session()->put(['bank' => $bank]);

        return $next($request);
    }
}
Solo.dmitry
  • 690
  • 8
  • 15
0

The following works for me on Laravel 5.2, is it causing errors on your app?

AppServiceProvider.php

class AppServiceProvider extends ServiceProvider
{
/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    \Session::put('lang', 'en_US');
    view()->share('lang', \Session::get('lang', 'de_DE'));
}

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    //
}
}

home.blade.php

<h1>{{$lang}}</h1>

Shows "en_US" in the browser.

kalatabe
  • 2,909
  • 2
  • 15
  • 24
  • \session not working, getting empty results, eg i tried this `print_r(\Session::all());` no results – Qazi Feb 10 '16 at 12:40
  • 1
    @Kaloyan: are you sure you are not setting the variable somewhere else? I don't think you can access the session from a service provider since it's started in a middleware – Moppo Feb 10 '16 at 16:15
  • @Moppo, yes, I'm not setting this anywhere else, maybe it's something that changed between 5.1 and 5.2? – kalatabe Feb 10 '16 at 18:28
  • 4
    it seems that you can write session from a service provider, but you can't read it. In your case it works as you are reading the value you're setting directly on the same request. But if you remove the `\Session::put('lang', 'en_US');` line and make another request, the value will not be read from the session – Moppo Feb 11 '16 at 13:55