2
votes

When a resource controller is created in Laravel like this:

Route::resource('foo', 'FooController');

We get URLs like:

  • foo/create
  • foo/store
  • foo/{id}/edit
  • foo/{id}/update
  • ...

I would like to translate some of these routes to get something like:

  • foo/nouveau
  • foo/store
  • foo/{id}/modifier
  • foo/{id}/update

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.

4

4 Answers

2
votes

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.

0
votes

As far as I know it's not possible using resource method. You will need to create those routes manually using trans / Lang::get, for example:

Route::get('foo/{id}/'.trans('routes.edit'), 'FooController@edit');

The names you can pass here in 3rd parameters are for named routes and don't have anything in common with urls, if you used something named routes as you showed you can now use:

URL::route('nouveau', 1);

and it will generate foo/1/edit url. If you didn't use here names you should use then:

URL::route('foo.edit',1);

to create this url but it's the only difference, no url impact here.

0
votes

This answer is based on Laravel documentation at: https://laravel.com/docs/5.7/controllers#restful-localizing-resource-uris

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

-1
votes

I believe your are just missing the namespacing on the routes. When Laravel generates a resource it creates the namespace as well. So instead of foo.create you would have foo.nouveau.

Route::resource('foo', 'FooController', array(
  'names' => array(
    'create'  => 'foo.nouveau',
    'edit'    => 'foo.modifier'
  )
));

This can also be referenced here in the Laravel docs