Bablu Ahmed
Bablu Ahmed

Reputation: 5010

url() vs route() in Laravel 5.6

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

<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:

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

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

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.

Upvotes: 7

Views: 18611

Answers (3)

Siraj Ahmed
Siraj Ahmed

Reputation: 144

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,

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

<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::get('/article/create', 'createController@create');

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

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

Upvotes: -1

Kuldeep Mishra
Kuldeep Mishra

Reputation: 4040

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.

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

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

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

Upvotes: 22

Sahil Gupta
Sahil Gupta

Reputation: 597

Define route with name

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

Now, url() will use path of route;

url('/article/create');

and route() will use name of route

route('article.create');

Upvotes: 12

Related Questions