9

For example, I have defined routes like this:

$locale = Request::segment(1);

Route::group(array('prefix' => $locale), function()
{
  Route::get('/about', ['as' => 'about', 'uses' => 'aboutController@index']);
}

I want to generate links for several locales (en, de, es,...). When I try to provide prefix parameter like this

$link = route('about',['prefix' => 'de']);

I got link like this example.com/en/about?prefix=de How to provide prefix param to got link like this example.com/de/about

4
  • I don't think laravel route has any function for that. Checkout this link Commented Dec 25, 2015 at 9:52
  • 1
    Yes there is check docs Commented Dec 25, 2015 at 9:55
  • Yes, @Uchiha it could be possible with named parameter in property prefix for that group. Thanks Commented Dec 25, 2015 at 9:58
  • @KalanjDjordjeDjordje #sixFingersMan answer is correct Commented Dec 25, 2015 at 10:06

5 Answers 5

15

You can play around with something like this perhaps.

Route::group(['prefix' => '{locale}'], function () {
    Route::get('about', ['as' => 'about', 'uses' => '....']);
});

route('about', 'en');  // http://yoursite/en/about
route('about', 'de');  // http://yoursite/de/about
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

$locale = Request::segment(1);

Route::group(array('prefix' => $locale), function()
{
    Route::get('/about', ['as' => 'about', 'uses' => 'aboutController@index']);
}

And while providing a link, you can use url helper function instead of route:

$link = url('de/about');

If you want more generic, use this in controller/view:

 $link = url($locale.'/about');

where $locale could be en,de,etc

Comments

0

You can do like this :

Route::group(['prefix'=>'de'],function(){
    Route::get('/about', [
       'as' => 'about',
       'uses' => 'aboutController@index'
    ]);

});

Now route('about') will give link like this : example.com/de/about

1 Comment

I don't need to define route. I need to get url for route with other prefix.
0

you can use the prefix method within a Route group to define a common prefix for a group of routes.

Route::group(['prefix' => 'admin'], function () {
    // Your admin routes here
    Route::get('dashboard', 'AdminController@dashboard');
    Route::get('users', 'AdminController@users');
    // ...
});

In the above example, all routes defined within the Route::group will have the "/admin" prefix.

Official Documentation: https://laravel.com/docs/10.x/routing#route-groups

Comments

-1

You can simply achieve it like as

Route::group(['prefix' => 'de'], function () {
    Route::get('about', ['as' => 'de.about', 'uses' => 'aboutController@index']);
});

And you can use it like as

$link = route('de.about');

4 Comments

de is not necessary in child routes
sorry, I little improved a question. Problem is how to generically change prefix while call route function
But its easy to identify @BasheerAhmed
@BasheerAhmed It won't

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.