0

I'm making a new Service called Factures in App\Services\Factures. I created the \App\Services\Factures\FacturesServiceProvider:

public function register() {
    $this->app->bind('factures', function ($app) {
        return new Facture;
    });
}

public function boot() {
    // laod Routes
    $this->loadRoutesFrom(__DIR__ .'/Http/routes.php');

    // load Views
    $this->loadViewsFrom(__DIR__ . '/views', 'factures');
}

I registered my provider everything works fine expect the Auth::user() in returns me  null in the views and the routes.php.

How can I get access to the Auth() in custom service?

2
  • Is the session middleware added in your Kernel file in the web middleware array? Is the user logged in? Commented Apr 2, 2019 at 16:07
  • Yes, all other routes from routes/web.php workes fine and I can access the Auth(). Only in the new service I can't access the Auth() Commented Apr 2, 2019 at 16:32

1 Answer 1

1

This post resolved my problem: User Auth not persisting within Laravel package

I figure out that Laravel  apply to the default routes/web.php file a middleware called 'web' And doesn't apply this group to external package routes loaded via service provider's.

So my routes in the custom file should be in web middleware:

Route::group(['middleware' => ['web']], function () {
    Route::get('testing-services', function(){
        dd(Auth::user());
        // output is valid
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.