Laravel 使用命名路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23140787/
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 09:22:44  来源:igfitidea点击:

Laravel using named routes

phplaravel

提问by enchance

Regarding the use of named routes, these 2 lines allow me to access the same page so which is correct?

关于命名路由的使用,这两行允许我访问同一页面,哪个是正确的?

// Named route
Route::get('test/apples', array('as'=>'apples', 'uses'=>'TestController@getApples'));

// Much simpler
Route::get('apples', 'TestController@getApples');

Is there any reason I should be using named routes if the latter is shorter and less prone to errors?

如果命名路由较短且不易出错,我是否应该使用命名路由?

采纳答案by Sam

The only advantage is it is easier to link to, and you can change the URL without going through and changing all of its references. For example, with named routes you can do stuff like this:

唯一的优点是更容易链接,您可以更改 URL,而无需遍历和更改其所有引用。例如,使用命名路由,您可以执行以下操作:

URL::route('apples');
Redirect::route('apples');
Form::open(array('route' => 'apples'));

Then, if you update your route, all of your URLs will be updated:

然后,如果你更新你的路由,你的所有 URL 都会被更新:

// from
Route::get('test/apples', array('as'=>'apples', 'uses'=>'TestController@getApples'));

// to
Route::get('new/apples', array('as'=>'apples', 'uses'=>'TestController@getApples'));

Another benefit is logically creating a URL with a lot parameters. This allows you to be a lot more dynamic with your URL generation, so something like:

另一个好处是在逻辑上创建一个带有很多参数的 URL。这使您可以更加动态地生成 URL,例如:

Route::get('search/{category}/{query}', array(
    'as' => 'search',
    'uses' => 'SearchController@find',
));

$parameters = array(
    'category' => 'articles',
    'query' => 'apples',
);

echo URL::route('search', $parameters);
// http://domain.com/search/articles/apples

回答by The Alpha

Named routes are better, Why ?

命名路由更好,为什么?

It's always better to use a named routebecause insstsead of using the urlyou may use the nameto refer the route, for example:

使用 a 总是更好,named route因为 insstsead 使用url您可以使用name来引用路线,例如:

return Redirect::to('an/url');

Now above code will work but if you would use this:

现在上面的代码将工作,但如果你会使用这个:

return Redirect::route('routename');

Then it'll generate the urlon the fly so, if you even change the urlyour code won't be broken. For example, check your route:

然后它会url即时生成,因此,即使您更改url代码也不会被破坏。例如,检查您的route

Route::get('apples', 'TestController@getApples');
Route::get('apples', array('as' => 'apples.show', 'uses' => 'TestController@getApples'));

Both routes are same but one without nameso to use the route without name you have to depend on the url, for example:

两条路线相同,但没有name一条路线,因此要使用没有名称的路线,您必须依赖于url,例如:

return Redirect::to('apples');

But same thing you may do using the route name if your route contains a name, for example:

但是,如果您的路线包含名称,您可以使用路线名称做同样的事情,例如:

return Redirect::route('apples.show');

In this case, you may change the urlfrom applesto somethingelsebut still your Redirectwill work without changing the code.

在这种情况下,您可以将urlfrom更改为applessomethingelse但您仍然Redirect可以在不更改代码的情况下工作。

回答by LP Papillon

The only reason to name the route is if you need to reference it later. IE: from your page in a view or something, check whether you are in that route.

命名路由的唯一原因是您以后需要引用它。IE:从您的页面中的视图或其他内容,检查您是否在该路线中。