Laravel 中 URL::to 和 URL::route 的区别

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

Difference between URL::to and URL::route in laravel

phplaravellaravel-5laravel-routing

提问by Kiran Subedi

What is the difference between

之间有什么区别

<a href=" {{ URL::route('/account/register') }}" >Register 1 </a>

and

<a href=" {{ URL::to('/account/register') }}" >Register 2 </a>

I defined the routes.php as

我将 routes.php 定义为

Route::get('/account/register','RegisterController@create');

When I click on 'Register 1' I got the following error

当我单击“注册 1”时,出现以下错误

Route [/account/register] not defined.

But when I click on 'Register 2' ,it goes to the

但是当我点击“注册 2”时,它会转到

RegisterController@create 

回答by Limon Monte

URL::routegets the URL to a named route. So in your case, if you name your route like this:

URL::route获取指定路由的 URL。所以在你的情况下,如果你像这样命名你的路线:

Route::get('/account/register', [
    'name' => 'register', 
    'uses' => 'RegisterController@create'
]);

then you will be able to use

然后你就可以使用

<a href="{{ URL::route('register') }}" >Register 1</a>

in blade templates.

在刀片模板中。

回答by Severian

Url::route is used only if you have named routes. So if I called my route "my route" then I could call it like so: URL::route('my route');

Url::route 仅在您命名路由时使用。因此,如果我称我的路线为“我的路线”,那么我可以这样称呼它: URL::route('my route');

But if you want to direct to a route that only has a destination and is not named, then you should use URL::to

但是如果你想指向只有一个目的地并且没有命名的路由,那么你应该使用 URL::to