Route and Auth helpers are not available in service providers #14
Unanswered
jameswagoner
asked this question in
General
Replies: 2 comments
-
Even if you use it inside namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class NavigationMenuServiceProvider extends ServiceProvider
{
/** Register services ...*/
public function register(): void {}
/** Bootstrap any application services ...*/
public function boot(): void
{
$this->app->booted( function() {
$nav = app( \Spatie\Navigation\Navigation::class )
->add( 'Home', route( 'welcome' ) );
$permisos = [
'administrar usuarios',
'administrar roles',
'administrar permisos',
];
if ( auth()->user()?->hasAnyPermission( $permisos ) ) {
$nav->add( 'Admin Panel', route( 'adminpanel.dashboard' ), function( \Spatie\Navigation\Section $section ) use ( $permisos ) {
$section->add( 'Dashboard', route( 'adminpanel.dashboard' ), null, [ 'active' => request()->routeIs( 'adminpanel.dashboard' ) ] );
if ( auth()->user()?->hasPermissionTo( $permisos[ 0 ] ) ) {
$section->add( 'Users', route( 'adminpanel.users' ), null, [ 'active' => request()->routeIs( 'adminpanel.users' ) ] );
}
if ( auth()->user()?->hasPermissionTo( $permisos[ 1 ] ) ) {
$section->add( 'Roles', route( 'adminpanel.roles' ), null, [ 'active' => request()->routeIs( 'adminpanel.roles' ) ] );
}
if ( auth()->user()?->hasPermissionTo( $permisos[ 2 ] ) ) {
$section->add( 'Permisos', route( 'adminpanel.permissions' ), null, [ 'active' => request()->routeIs( 'adminpanel.permissions' ) ] );
}
$section->add( 'About', route( 'adminpanel.about' ), null, [ 'active' => request()->routeIs( 'adminpanel.about' ) ] );
} );
}
$nav->addIf( auth()->user()?->hasRole( 'administrador' ), '', '', function( \Spatie\Navigation\Navigation $navigation ) {
$navigation->add( 'Admin', route( 'admin.dashboard' ) );
} );
$nav->add( 'About', route( 'adminpanel.about' ) );
View::composer( '*', function( $view ) use ( $nav ) {
$navbar_menu = $nav;
$view->with( 'navbar_menu', $navbar_menu );
} );
} );
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Best to put this in middleware if you need access to auth(). You cannot get the authorized user in a service provider due to the order in which the laravel flows. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When trying to use
route()
in any service provider, (ie:AppServiceProvider
,RouteServiceProvider
, or even a custom provider) you receive a route not found error.auth()
orAuth::
when logged in will returnnull
.Seems that these are not resolved till after the app is booted. One solution is using
$this->app->booted()
with a callback to build the navigation but that seems silly to me. Luckily my application is leveraging Inertia for it's frontend so my solution has been to build my navigation inApp\Http\Middleware\HandleInteriaRequests
middleware.This issue is not the packages fault; however, the readme should remove the recommendation of using route and auth helper methods within a service provider.
Beta Was this translation helpful? Give feedback.
All reactions