When a resource controller is created in Laravel like this:
Route::resource('foo', 'FooController');
We get URLs like:
I would like to translate some of these routes to get something like:
This code is working:
Route::resource('foo', 'FooController', array(
'names' => array(
'create' => 'nouveau',
'edit' => 'modifier',
...
)
));
The problem here is the edit route: I don't know how to make it works with an {id} like foo/{id}/modifier.
Checkout my package: https://github.com/doitonlinemedia/TranslatableRoutes pretty easy to use.
You can call the resource routes like:
TranslatableRoute::resource('recipe', 'recepten', 'RecipeController');
Where the second argument is the translated name and the first defines the name of your routes.
This answer is based on Laravel documentation at: https://laravel.com/docs/5.7/controllers#restful-localizing-resource-uris
By default, Route::resource will create resource URIs using English verbs. If you need to localize the create and edit action verbs, you may use the Route::resourceVerbs method. This may be done in the boot method of your AppServiceProvider:
use Illuminate\Support\Facades\Route;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Route::resourceVerbs([
'create' => 'nouveau',
'edit' => 'modifier',
]);
}
Once the verbs have been customized, a resource route registration such as Route::resource('foo', 'FooController') will produce the following URIs:
/foo/nouveau
/foo/{id}/modifier
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With