Laravel 6 Routing | Laravel Tutorial

In this laravel 6 routing tutorial, we will discuss laravel routings. How you can create SEO friendly URLs with laravel routing.

Laravel Routing is very easy and simple to use because of its flexibility. Before we start the laravel routing tutorial, you must know what’s is route in laravel.

What is Laravel Routing?

Routing is a way to create a request URL for your application. The best thing about Laravel routing is that you are free to define your routes the way you want it to look like.

In Laravel, all requests are mapped with routes, all routes are created inside the root folder. For your web application, you can define application-related routes in the web.php file while all routes for the API are defined in the epi.fap file.

When you download the fresh laravel set up and you will open the laravel route.php file.

it looks like.

Route::get('/', function () {
return view('welcome');
});

The routes you define in Laravel are very simple, you start a new route with Route:: facade and then after the type of request that you want to assign to that route. Below, are all the route methods or request types that you can use in Laravel.

  • get
  • post
  • put
  • delete
  • patch
  • options

Routing with Controller Methods

Let’s take an example of laravel routes with controllers. If you pass the controller method name in route and the user will hit the route, the method of the defined controller will be executed to performing the actions.

Route::get('/home', '[email protected]');

In the above example code, the route starts with the Route:: facade and the method will use the route is get.

Laravel Routing With Parameters

let’s take an example of routing with parameters. In this routing method, you can pass additional parameters en route. Let’s create a route that will take a number and then print it on the screen.

Route::get('/page/{no}', function ($no) {
echo "Your page no is ". $no;
});

In the above example code, we created a route that will take a number parameter in the route method.

Laravel Routing With Optional Parameters

Let’s take an example of routing with optional parameters in laravel. You can add a ? in your route with parameters, it will become an optional parameter.

Route::get('/page/{test?}', function ($test = 'tutsmake') {
echo "Welcome to ". $test;
});

Routing Naming in Laravel

Laravel provides an easy way to name your routes by which we don’t have to hard code the URI in our Blade views.

You can determine the route name using the name method on the route example below.

Route::post('/form', '[email protected]')->name('contact');

Now, you can set easily the from action method like below:

<form action="{{ route('contact') }}" method="get" accept-charset="utf-8">
    
</form>

Laravel Group Routing

Laravel provides an easy way to groups your routes. It will allow you to share route properties, such as prefix, middlewares, and namespaces across all routes within that group.

Route::group(['prefix' => 'posts', 'as' => 'posts.'], function () {
Route::get('/', '[email protected]')->name('index');
Route::get('/create', '[email protected]')->name('create');
Route::post('/store' '[email protected]')->name('store');
});

Laravel Route Groups With Namespaces

Laravel also provides an easy way to namespace routing, you will see the example below for that:

We can also define the PHP namespace for the controllers using the route groups.

Route::group(['namespace' => 'Post'])

Route::group(['namespace' => 'Post','prefix' => 'posts', 'as' => 'posts.'], function () {

	Route::get('/', '[email protected]')->name('index');

	Route::group(['middleware' => ['auth']], function () {
		Route::get('/create', '[email protected]')->name('create');
		Route::post('/store' '[email protected]')->name('store');
	});
});

Laravel artisan route:list

Using the below command, you can view the names, URLs, and middlewares used by the routes which is quite handy for debugging purposes.

php artisan route:list

Conclusion

In this tutorial, you have learned laravel routings. How many types of routing provides laravel and how to use it.

If you want to know more about the laravel routing click here

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *