How to Generate Url From Name Routes in Laravel 6 ?

Laravel 6 , Laravel

Nicesnippets

3094

29-11-2019


How to Generate Url From Name Routes in Laravel 6 ?

Hi Guys,

In this tutorial , i will give you simaple example of generate url to named routes in laravel 6. we are create examples of create url to named route.you can generate url from name routes in laravel 6.

All Laravel 6 routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. The routes/web.php file defines routes that are for your web interface.

Example 1: Basic Url To Routes

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

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

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

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

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

The generated URL looks like:

http://example.com/home

Example 2: Simple Parameter Url To Routes

Laravel 6 provides a global route() function that gets the URL to a named route. The first Parameter is the route name (string). Depending on the route you’re trying to access you may also need to pass in an array of parameters as the second argument.

it would look as routes/web.php file defines routes that are for your 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

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

it would look as routes/web.php file defines routes that are for your 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

It will help you....


Recommended Posts