0

On laravel 9 site I need to get the current user in AppServiceProvider.

I know that usually current user is not available in AppServiceProvider.

I found this branch Laravel - How to get current user in AppServiceProvider it propose a way like :

...
use Illuminate\Support\Facades\View;

public function register()
{
    //compose all the views....
    view()->composer('*', function ($view) 
    {
            \Log::info('View in register ');    
    });  
}


public function boot()
{
    //compose all the views....
    view()->composer('*', function ($view) 
    {
            \Log::info('View in boot ');    
    });  
}

But none of the events is triggered and I do not see any log messages. Actually that is API project with passport and it does not use views... Do I see set additive options/packages/etc ?

"laravel/framework": "^9.26.1",
"laravel/passport": "^10.4.1",

Thanks in advance!

Petro Gromovo
  • 1,755
  • 5
  • 33
  • 91
  • 1
    if there are no views being rendered then there are no views to compose .... the next thing is why do you need access to the current user in a service provider? – lagbox Dec 04 '22 at 17:28
  • I need to switch to different class implementation depending of field of logged user. Usually that is done in a service provider – Petro Gromovo Dec 05 '22 at 03:01
  • If there to add fake views to compose ? Or are there better way for this task? – Petro Gromovo Dec 05 '22 at 07:38
  • 1
    view composers are for sharing data with a view, that is all they are for ... you can bind things to the container at any time – lagbox Dec 05 '22 at 14:12
  • @lagbox, "you can bind things to the container at any time " - Do you mean that I can do it in some other way ? Please, detalize... – Petro Gromovo Dec 07 '22 at 10:31

0 Answers0