Explained Laravel Routing

Routing in Laravel
In this post, we are going to explain how Laravel Routing. It is a free open source PHP framework and used by a large community. Laravel has a MVC based structure.

Laravel Routing is a way to explain, which URL will execute. It creates a request URL like for a specific identifier (Id, Name, or any other optional parameters). In laravel routing help to map all the request URL.

Laravel Routing Tutorial

In the Laravel routes folder contain two routes files, one is api.php, which is used to API routes and another is web.php, which is used for routes for the web application.

Routing in Laravel includes various methods. We explore each one with an example code step by step.

  • Basic Routing
  • Route Parameters
  • Named Routes
  • Route Groups
  • Route Model Binding

1) Basic Routing

In Laravel 5.5 all the routes are registered in routes folder. These routes files are automatically created by the framework when you configured Laravel on your server. The web.php contains all routes information of your web application.

In this example, the GET request route method will execute. When you hit the Laravel home page URL then a function calls the view function to fetch the home.balde.php file under resources/views/ directory. Using this basic routes method, it displays the home.balde.php file HTML content.

Other Basic Routes Methods:

Basic routing has other available routes methods like Get, Put, Post, Delete, Patch, Options. See below example route code for these:

If you need to register multiple HTTP verbs than we can use it like that:

2) Route Parameters

When we need to capture the parameter passed to the URL, route parameters work for it. We can achieve this in two ways.

Required Parameter:

Using this method its compulsory to capture the passed parameter. For example, getting the user ID from the URL.

Optional Parameter:

Sometimes we need to make route parameters as optional, then we use this method. Just place ‘?’ symbol after the parameter name. Make sure that you have pass default value to that variable.

3) Named Routes

In this laravel routing method, we can set any name to route. This feature requires some time, for example, you want to redirect a user one to another route than no need to define complete redirect URL, just give its name.

If you have using the names routes method and want to change the URL structure then only change the web.php file. See the example code below:

In this example ‘task’ is the route name and ‘route(‘task’,[‘id’ => 2])’ is the output value.

4) Route Groups

In route groups, you can combine large number of routes in group to share route attributes like namespaces or middleware. No need to define an attribute for each route.

In the below example, we combine the ‘auth’ middleware with URL prefix ‘admin’.

5) Route Model Binding

Using this method you can inject a model ID into your route or controller action. This method has two ways one in Implicit binding and another one is Explicit Binding.

In the above code, by using $post we can inject the post object. Inject the ‘post’ model into a route controller.

Conclusion:

Routing is an important component of a web application, its quickly allow to manage various URLs structure. Although handling routing is not easy during different situations. But the Laravel routing feature makes the process very easy. We hope this tutorial helps you to understand the Laravel routing concept.

Recommended Posts For You

About Harish

I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me at - [email protected]

View all posts by Harish

Leave a Comment

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