The provider boot method is fired after all providers register so session should work.
Laravel 5 session data is not accessible in the app boot process
In Laravel 5, It seems you can't access the session data in a ServiceProvider::boot(), the Session::get() just returns null while the data exists in the session.
In a package ServiceProvider how can I access to the session data?
I think I should hook to an event, is there an event about loading session in laravel 5, if not what event should I use? I want to share a variable in my views.
The provider boot method is fired after all providers register so session should work.
I've checked it, you're right. You could add a middleware.
What about App::booted()?
I have submitted a proposal here https://github.com/laravel/framework/issues/7906
For now my workaround was to create a new StartSession middleware in app.
protected function startSession(Request $request)
{
$session = parent::startSession($request);
\Event::fire('session.started');
return $session;
}
Then in Kernel.php
protected $middleware = [
// Replace
'Illuminate\Session\Middleware\StartSession',
// With
'App\Http\Middleware\StartSession',
]
Then put this in the service providers boot method
$this->app['events']->listen('session.started', function() {
dd($this->app['session']->all());
});
Hope that helps someone else out, it was a very frustrating issue for me.
Edit: just seen your comment franzliedke, the booted method doesn't work either from what I can remember.
It looks like you also need to include
$this->app->singleton('App\Http\Middleware\StartSession');
In the app service provider. I don't understand why though. Can anyone explain that?
Lets keep this issue going. I believe this is a true bug that should be resolved.
it seem lavarel 5.x not working fine with persistent session http://prntscr.com/7lyars i tried and got NULL
Hi guys,
I know it was too late to respond but I face the session issue in AppServiceProvider Class in (Laravel 5.1) and fixed with below code. use Illuminate\Support\Facades\Session;
and then in boot function add this line of code your session will be set through the app.
Session::put('someKey', 'someValue');
Thanks
We can get session value or data of a loginned user at AppServiceProvider. The following we will show how to get, share values of session and loginned user to all views.
Code:
namespace App\Yourapp\Younamespace;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\View;
use App;
class AppServiceProvider extends ServiceProvider{
public function boot() {
$auth = $this->app['auth']; // get Laravel's Auth facades
view()->composer('*', function($view) use (&$auth) { // this function coding style is php callback and you can pass variables at boot() to this callback function.
$user = $auth->user(); // get the loginned user
if ( empty(Session::get('sessionDataWithAssignedKeyYouWant', '')) {
Session::put('sessionKeyYouWant', 'sessionDataYouWant'); // set session data
}
$mySessionDataWithAssignedKey = Session::get('sessionKeyYouWant', 'sessionDataYouWant'); // get session data
$view->with(['user'=> $user, 'myVariable' => $mySessionDataWithAssignedKey]); // share these values to all views.
});
}
public function register() {
}
}
You can change the first parameter at view()->composer() from star sign to what you want. For example, user.* means all views under user directory or this way *.checkout means all view directories has named checkout view. But when you change from star sign to what you want view, you should rather to get and set session data in middleware instead of do them in AppServiceProvider.
Enjoy it.
Inpired by these links:
-
https://laracasts.com/discuss/channels/general-discussion/laravel-5-session-data-is-not-accessible-in-the-app-boot-process (just this link)
Reference: my blog article: http://blog.xuite.net/yhunglee/blog/549548212
Here is how you can access session in AppServiceProvider boot method.
I stumbled upon on this issue as well and approached it in another way. For what its worth I used IoC to call the sessions from there and injected it on my partial views. Would love to hear feedback for this.
As I said on StackOverflow, sometimes people seem to forget Laravel is built upon PHP, and completely forget how to use PHP on its behalf.
Please or to participate in this conversation.