Laravel 5.6 中的 url() 与 route()

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

url() vs route() in Laravel 5.6

laravel

提问by Bablu Ahmed

In my case, what is the difference between url() and route() in Laravel 5.6, two URIs are given below:

就我而言,Laravel 5.6 中 url() 和 route() 之间的区别是什么,下面给出了两个 URI:

<a href=" {{ route('/article/create') }}" >Create post 1 </a>

and

<a href=" {{ url('/article/create') }}" >Create post 2 </a>

I defined them in web.php as follows:

我在 web.php 中定义它们如下:

Route::post('/article/create','ArticleController@create');

When I click on 'Create post 1' I got the following error:

当我单击“创建帖子 1”时,出现以下错误:

Route [/article/create] not defined. 

I am not familiar with Laravel (just basic) so I am sorry if the question is some kind of obvious.

我不熟悉 Laravel(只是基本的),所以如果问题有点明显,我很抱歉。

回答by Kuldeep Mishra

Let's suppose you are using the same URL in 10 different place and later on, you decide to change it. If you're using named route you have to modify URL only in route file and all links will still work.

假设您在 10 个不同的地方使用相同的 URL,稍后您决定更改它。如果您使用命名路由,则只需在路由文件中修改 URL,所有链接仍然有效。

Route::post('/student/create', 'ArticleController@create')->name('student.create');

Now, instead of passing path to the url() function, you can use route name:

现在,您可以使用路由名称,而不是将路径传递给 url() 函数:

route('student.create'); // instead of url('/student/create');

回答by Sahil Gupta

Define route with name

用名字定义路由

Route::post('/article/create','ArticleController@create')->name('article.create');

Now, url()will use path of route;

现在,url()将使用路由路径;

url('/article/create');

and route()will use name of route

并且route()将使用的路线名称

route('article.create');

回答by Siraj Ahmed

So first of all i want to write a difference between URL's and Route in Laravel 5.6 In laravel Url's is to link the different pages of the website For example,

所以首先我想写一个Laravel 5.6中URL和Route的区别在laravel Url中是链接网站的不同页面例如,

I want to go the create page in my website so the Url's is this,

我想去我网站的创建页面,所以 URL 是这样的,

<a href=" {{ url('/article/create') }}" >Create post 2 </a>

And the second is Route so in laravel Route accept the Url's and checked if Url's is right and give to the result

第二个是 Route 所以在 laravel Route 中接受 Url 并检查 Url 是否正确并给出结果

Route::get('/article/create', 'createController@create');

and if you want use Url's over Route and Route over Url's like this

如果你想使用 Url's over Route 和 Route over Url's 这样

<a href=" {{ route('/article/create') }}" >Create post 1 </a> 

<a href=" {{ url('/article/create') }}" >Create post 2 </a>

you can use with Alias Route name

您可以与别名路由名称一起使用

Route::get('/article/create', 'createController@create')->name('create');