Laravel how to add group prefix parameter to route function

15,922

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
Share:
15,922
Kalanj Djordje Djordje
Author by

Kalanj Djordje Djordje

Updated on July 15, 2022

Comments

  • Kalanj Djordje Djordje
    Kalanj Djordje Djordje almost 2 years

    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

  • Kalanj Djordje Djordje
    Kalanj Djordje Djordje over 8 years
    I don't need to define route. I need to get url for route with other prefix.
  • Basheer Kharoti
    Basheer Kharoti over 8 years
    de is not necessary in child routes
  • Kalanj Djordje Djordje
    Kalanj Djordje Djordje over 8 years
    sorry, I little improved a question. Problem is how to generically change prefix while call route function
  • Narendrasingh Sisodia
    Narendrasingh Sisodia over 8 years
    But its easy to identify @BasheerAhmed
  • Narendrasingh Sisodia
    Narendrasingh Sisodia over 8 years
    @BasheerAhmed It won't