memele
3 months ago

Route::current()->parameter() in AppServiceProvider

Posted 3 months ago by memele

Creating subdomains has broken most of my application :D I have a forum with subforums (as subdomains) and I want to display the current subdomain in the header (in layouts.app view). I already have a View composer in AppServiceProvider that shares some variables to all views. I also want to share the current subdomain (forum). I can fetch it easily in any controllers like this:

$subdomain = Route::current()->parameter('forum');
$forum = Forum::where('slug', $subdomain)->first();

But it doesn't work in the AppServiceProvider

View::composer('*', function ($view) {
    $subdomain = Route::current()->parameter('forum');
       $forum = Forum::where('slug', $subdomain)->first();

    View::share([
                'forum' => $forum
            ]);
}

dd(Route::current()->parameters()); returns []

What is this not working?

Please sign in or create an account to participate in this conversation.