简体   繁体   中英

Laravel localization and named routes in case of nested Route::group's

I need to pass language code via url for localization purposes, but encountered problem with nested Route::group .

My routes look like this at the moment:

Route::group(['prefix' => 'employee', 'namespace' => 'Employee'], function()
{
    Route::resource('user', 'UserController', ['only' => ['show', 'index']]);
});

This means that I have named routes like employee.user.show , employee.user.index etc. and URL like this /employee/user/2 , /employee/user etc.

I need to leave route names as is, but change my URL to such /en/employee/user/2 , /en/employee/user etc., so that language code is always passed via URL.

When I try to wrap my routes with new Route::group like this:

Route::group(['prefix' => 'en'], function() {
    Route::group(['prefix' => 'employee', 'namespace' => 'Employee'], function()
    {
        Route::resource('user', 'UserController', ['only' => ['show', 'index']]);
    });
});

I get URLs in the form I need, but route names become en.employee.user.show , en.employee.user.index etc., what is very inconvenient, cause makes me pass language all the time. My problem is similar to the one described here https://github.com/laravel/framework/issues/1616

I can think now of a few solutions:

1) do not use resources at all and define all POST , GET etc. routes manually and use as for route naming;

2) use action('Employee\\UserController@show') instead of route('employee.user.show') and pass language as extra parameter all the time;

3) somehow override route method, so that every time to parameters is added one more paratemer named lang with language code as its value (uses locale like route('employee.user.show', ['lang' => 'en']) , or uses current site locale if no lang param provided manually), so every time I call this method I get URL like /employee/user/2?lang=en etc. (or receive this URL even not passing any lang param if current locale is set to en ).

On each solution:

1) first solution is ugly, because I have to write 7 lines with extra params instead of simple short one-line resource declaration;

2) second solution makes me write all the time all namespases, controller names and method names and also add language parameter - all this is too ugly also;

3) I need to somehow overrite UrlGenerator 's route method, so that I still can use route method, but with enchanced functionality. How can I override this method in Laravel 4.2?

In order to add Google-like lang parameter that is automatically attached to all URLs, it is needed to override route helper. Here are steps:

1) create some separate file, let say helpers_override.php and put is somewhere, eg into app folder;

2) in index.php file before registration of Auto Loader (that is before the line require __DIR__.'/../bootstrap/autoload.php'; ) you have to require your helpers_override.php (in my case like this require __DIR__.'/../app/helpers_override.php';

The very route helper that will add lang parameter to every route looks like this:

if ( ! function_exists('route'))
{
    /**
     * Generate a URL to a named route.
     *
     * @param  string  $name
     * @param  array   $parameters
     * @param  bool  $absolute
     * @param  \Illuminate\Routing\Route $route
     * @return string
     */
    function route($name, $parameters = array(), $absolute = true, $route = null)
    {
        $parameters = (array) $parameters;

        if ( ! key_exists('lang', $parameters))
        {
            $parameters['lang'] = App::getLocale();
        }

        return app('url')->route($name, $parameters, $absolute, $route);
    }
}

As a result we do not have to create separate Route::group and use ugly route names like en.employer.user.show etc., we still preserve clear and nice route helper, we do not have to pass lang params manually all the time. We can even make some more modifications and add lang parameter only in case if language is different from default one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM