Laravel 如何将组前缀参数添加到路由功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34461689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:53:32  来源:igfitidea点击:

Laravel how to add group prefix parameter to route function

phplaravelrouteslaravel-5

提问by Kalanj Djordje Djordje

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

我想为多个语言环境(en、de、es、...)生成链接。当我尝试提供这样的前缀参数时

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

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

我得到了这样的链接example.com/en/about?prefix=de如何提供前缀参数来获得这样的链接example.com/de/about

回答by lagbox

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

回答by Kiran Subedi

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

现在route('about')将给出这样的链接: example.com/de/about

回答by Narendrasingh Sisodia

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');

回答by Vikas

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:

在提供链接的同时,您可以使用 url 辅助函数代替路由:

$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

$locale 可能在哪里 en,de,etc