Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Golavt's avatar

Routes stop working when a query is added in the appServiceProvider

Hi guys, I am newbie to Laravel and this forum. I am currently learning Laravel (using version 10) and I have come across a problem. I want to have access to my menu on all front pages and I created the query in my appServiceProvider file like this:

       $lang  = request()->segment(1);
    if (!empty($lang) && $lang !== 'admin') {
        $activeLang =
            Language::where('language_domain', $lang)->firstOrFail();

        $navbar = Navigation::where('language_id', $activeLang->id)->where('navbar_status', 1)->get();
        View::share('navbar', $navbar);
    }

This code works on all routes (setlocale) except those that don't have a prefix and the prefix admin. Here is how I have my web.php setup:

	Route::group([], function () {
        require __DIR__ . '/auth.php';
        });
       Route::group([  'prefix' => 'admin',  'middleware' => ['auth', 'verified'],], function () {
          require __DIR__ . '/admin.php';
         });

      Route::get('/', function () { return Redirect::to("/sv");  });

      Route::group([''prefix' => '{locale}',  'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => ['setlocale']],
      function () {
      Route::get('/', [MainController::class, 'index'])->name('home');
       Route::get('/{single:slug}', [MainController::class, 'single'])
        ->where(['single' => '[a-z0-9\-]+'])->name('single');
	    Route::get('/{category:slug}/{post:slug}', [MainController::class, 'double'])
         ->where(['category' => '[a-z0 9\-]+', 'post' => '[a-z0-9\-]+'])->name('double');
          });

Could some one help me solve this. I am open to suggestions if there is a better way to solve it.

Best regards from Sweden.

0 likes
5 replies
krisi_gjika's avatar

"This code works on all routes (setlocale) except those that don't have a prefix and the prefix admin" - so it does not work on auth.php routes? Why don't these routes have a locale? What if the user wants to see the login page in a different language? Also I would suggest to use a default locale if none is found, instead of failing the application.

Anyhow the reason it does not work is bc with request()->segment(1); you never know if the first segment is what you expect, a locale, or something like login or register. Try something like: request()->route('locale', config('app.locale'));, you can remove the default value if you wish.

2 likes

Please or to participate in this conversation.