4

At the moment I have in my routes/web.php the following:

Route::group( [ 'prefix' => '{locale?}', 'middleware' =>\App\Http\Middleware\Locale::class ], function (\Illuminate\Routing\Router $router) {

    Route::get( '/', 'LandingController@index' )->name( 'home' );
    Route::get( '/hero/create', 'HeroController@create' )->name( 'hero.create' );
} );

This doesn't really work as it should be.

What I want is to have an url like this:

/create/hero    # should work with the default locale
/fr/create/hero # should use the french locale
/nl/create/hero # should use dutch locale
/               # should work with the default locale
/fr             # should use the french locale
/nl             # should use dutch locale

So I want the locale parameter optional at the beginning of the url. So far what I've managed to achieve is only to get the urls working when specifying the locale myself. I always get a not found message when I don't manually specify the locale.

I know I should be able to do it like this:

Route::get('/path/{id}/{start?}/{end?}', ['as' => 'route.name', 'uses' => 'PathController@index']);

public function index($id, $start = "2015-04-01", $end = "2015-04-30")
{
    // code here
}

But I think that's a but would mean I have to set the default locale in every controller which is a bit ugly in my opinion. Also, I think that this should be possible in a more elegant way in Laravel.

How can I set a default value for the locale prefix in my url?

  • Have you considered redirecting the `/` and `/create/hero` to the default locale version instead of serving real content at those urls? – sisve Feb 01 '18 at 16:24

1 Answers1

5

You have to think about routes generated deeply. And never use prefix as optional , so to make work all url give change in route like this

Route::get( '/', 'LandingController@index' )->name( 'home' );
Route::get( '/hero/create', 'HeroController@create' )->name( 'hero.create' );

Route::group( [ 'prefix' => '{locale}', 'middleware' =>\App\Http\Middleware\Locale::class ], function (\Illuminate\Routing\Router $router) {
    Route::get( '/', 'LandingController@index' )->name( 'home' );
    Route::get( '/hero/create', 'HeroController@create' )->name( 'hero.create' );
} );

Here is the short description in your way

/               # should work with first above route 
/create/hero    # should work with first above route
/fr/create/hero # should work with route inside prefix
/nl/create/hero # should work with route inside prefix
/fr             # should work with route inside prefix
/nl             # should work with route inside prefix

It can be solve either to put optional ({locale?}) at the last not in between, or you can just put route into a variable and put in both condition, I mean outside prefix and inside prefix.

$heroRoutes = function (\Illuminate\Routing\Router $router) {
    Route::get( '/', 'LandingController@index' )->name( 'home' );
    Route::get( '/hero/create', 'HeroController@create' )->name( 'hero.create' );
}
Route::group( [ 'middleware' =>\App\Http\Middleware\Locale::class ], $heroRoutes );
Route::group( [ 'prefix' => '{locale}', 'middleware' =>\App\Http\Middleware\Locale::class ], $heroRoutes );
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • I know I can do it like that. But when someone decides to share an url like /hero/create I don't think it should result in a 404. I'd like that route to be available as well and use the default language. Therefore I'd like the language parameter to be optional. –  Feb 01 '18 at 15:49
  • @SheperdOfFire: If you use `/hero/create` as you said , first word `hero` would be the value of `locale` not as you expected and you will get `404` error. I hope you got the point what I mean ? – Niklesh Raut Feb 01 '18 at 16:01
  • Yes I understand, but that would also mean I'd have to create every route twice. I was hoping there would be an automated solution for that –  Feb 01 '18 at 16:03
  • It can be solve either to put optional (`{locale?}`) at the last not in between, or you can just put route into a variable and put in both condition, I mean outside prefix and inside prefix. – Niklesh Raut Feb 01 '18 at 16:05
  • You can get some idea from other [source](https://github.com/laravel/framework/issues/9741) and [more](https://stackoverflow.com/questions/46032537/laravel-optional-prefix-routes-with-regexp) – Niklesh Raut Feb 01 '18 at 16:30