Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

url() vs route() in Laravel 5.6

Tags:

laravel

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.

like image 605
Bablu Ahmed Avatar asked May 23 '18 11:05

Bablu Ahmed


People also ask

What is the difference between URL and route in Laravel?

For a start it is using a named route. This means that the link between the form and its controller are not dictated by the url that the user sees. The next advantage is that you can pass additional parameters into the route helper and it will put those in the correct place in the form action.

What is URL () in Laravel?

url() Generates an absolute URL to the given path (code)Preserves any URL query string. {{ url('search') }} // http://www.example.com/search {{ url('search', ['qevo', 'laravel']) }} // http://www.example.com/search/qevo/laravel.

What is Route method in Laravel?

Laravel route model binding provides a convenient way to automatically inject the model instances directly into your routes. For example, instead of injecting a user's ID, you can inject the entire User model instance that matches the given ID.

What is the benefit of named routes in Laravel?

Named routing is another amazing feature of Laravel framework. Named routes allow referring to routes when generating redirects or Urls more comfortably. You can specify named routes by chaining the name method onto the route definition: Route::get('user/profile', function () { // })->name('profile');


1 Answers

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');
like image 171
Kuldeep Mishra Avatar answered Oct 13 '22 10:10

Kuldeep Mishra