Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: translate resource URLs

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.

like image 810
Flobesst Avatar asked Dec 18 '25 20:12

Flobesst


2 Answers

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.

like image 191
Tim van Uum Avatar answered Dec 21 '25 12:12

Tim van Uum


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

like image 21
MarijnK Avatar answered Dec 21 '25 11:12

MarijnK