How to Generate Url From Name Routes in Laravel 8?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
How to Generate Url From Name Routes in Laravel

Hello Dev’s
Today in this tutorial, i will give you a simple and short way how to generate url to named routes in laravel 8 application. Here we will create a examples of create url to named route. Also we can generate url from name routes in laravel 8 application.Here we know How to Generate Url From Name Routes in Laravel 8.

In Laravel 8 all routes are defined in our route files, so which are located in the routes directory. So these all files are automatically loaded by the laravel framework. The routes/web.php file defines all routes that are for our web interface.

Example 1: Basic Url To Routes

Laravel 8 also provides a global route() function that gets the URL to a named route. So the only one Parameter is the route name (string).

It can look as a routes/web.php file defines routes that we are for our web interface:

Route::get('home', 'HomeController@index')->name('home');

So it would look as an anchor tag within in our Blade template:

<a href="{{ route('home') }}">Simple url To route</a>

Now the generated URL looks like:

http://example.com/home
Example 2: Simple Parameter Url To Routes

Now we are see Laravel 8 provides a global route() function that will get the URL to a named route. At the first Parameter is the route name (string). On depending with the route we’re trying to access we may also need to pass in an array of parameters as the second argument.

So it would look as routes/web.php file also defines routes that are for our web interface:

Route::get('posts/{id}', 'PostsController@show')->name('post');

It would look as an anchor tag within in a Blade template:

<a href="{{ route('posts', [$id]) }}">Link to Resource {{ $id }}</a>

The generated URL looks like:

http://example.com/posts/1
Example 3 : Adding Additional Parameters

In laravel 8 provides a global route() function that gets the URL to a named route. The example also above passes a Adding Additional parameter ($post) and also ($comment) into the route function. Now In this case, the ‘show’ method that is being hit expects a Adding Additional argument – and this is correctly passed in. If need more arguments are required they will be passed in the order specified in the route() function in laravel.

Now it would look as routes/web.php file defines routes that we are for our  web interface:

Route::get('posts/{post}/comments/{comment}', 'CommentController@show')->name('comment');

it would look as an anchor tag within in a Blade template:

<a href="{{ route('comment', ['1', '2', 'par'=>'HELLO', 'par2'=>'Goodbye']) }}">Comment</a>

The generated URL looks like:

http://example.com/posts/1/comments/2?par=HELLO&par2=Goodbye

Read Also : How to Disabled Specific dates and Friday,Saturday In Bootstrap Datepicker ?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →