Search code examples
phplaravel-5.3laravel-blade

route() generates url without locale prefix


I'm trying to generate a url through the route() helper in one of my (Blade) templates.

My web.php file looks like this (dummy routes, but the structure is the same)

// Generic routes
Route::get('/test', 'HomeController@test')->name('test');

// Localized routes
Route::group([
    'prefix' => LaravelLocalization::setLocale(),
    'middleware' => [ 'localeSessionRedirect', 'localizationRedirect' ]
    ], function() {
        Route::get(LaravelLocalization::transRoute('routes.product'), 'ProductController@product')->name('product');
});

Now, if I try to do this in the template of my test-route:

{{ route('product', ['slug' => 'product-slug']) }}

Then it doesn't generate this url:

https://my-domain.com/en-GB/product/product-slug

But it generates this url (note the missing locale):

https://my-domain.com/product/product-slug

However, ... If I move the test route into the Localized route group, then the url generator DOES add the locale.

Is this expected behaviour? Does this mean that I have to add all my routes into the Localized route group, even though some routes aren't translated?

Thanks!


Solution

  • I've 'fixed' this by using the getLocalizedURL() helper of the mcamara/laravel-localization package instead of the route() helper.

    {{ LaravelLocalization::getLocalizedURL(App::getLocale(), route('product', ['slug' => 'product-slug'])) }}