1

I have this group in my route: }

Route::group(['prefix' => 'v1/en'], function() {}

is it possible to change the 'en' segment uri in a parameter, so it can be changed depending on the request? Something like this:

Route::group(['prefix' => 'v1/{lang}'], function() {}

2 Answers 2

3

Yes you can, $lang will be available if you define your route group like this:

Route::group(['prefix' => 'v1/{lang}'], function() {
    // ...
});

or with new syntax:

Route::prefix('v1/{lang}')->group(function() {
    // ...
});

You can access it anywhere with:

request()->route()->parameter('lang');

You might want to checkout packages like this: Localization for Laravel

They basically do the same, and have nice middleware implementations to set locales etc.

Sign up to request clarification or add additional context in comments.

2 Comments

my lang is not a parameter, is a uri part like v1/en
If you make it like 'v1/{lang}' it is?
1

How about to generate dynamically another route group with prefix $lang inside of group with v1 prefix?

$langs = ['en', 'de', 'it'];

Route::group(['prefix' => 'v1'], function() use ($langs) {
  foreach($langs AS $lang) :

    Route::group(['prefix' => $lang], function() {

      Route::get('something', 'SomethingController@list');

    });

  endforeach;
});

or same logic (taken from here):

Route::group(['prefix' => 'v1'], function() use ($langs) {
    Route::group(['prefix' => '{lang}'], function() {
      Route::get('something', 'SomethingController@list');
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.