Explained Routing in Laravel 5.5

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

Routing is a way to explain that which URL will execute. Its create 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 5.5 Routing

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

Routing in Laravel includes various method. We are explore each one with 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 automatic created by framework when you configured Laravel on your server. The web.php contain all routes information of your web application.

In this example, GET request route method will execute. When you hit the Laravel home page URL, than a function call the view function to fetch the home.balde.php file under resources/views/ directory. Using this basic routes method, it display 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 achive this via 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:

Sometime we need to make route parameter as a optional, than we are 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 route method, we can set any name to route. This feature require 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 names routes method and want to change the URL structure than 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 attribute like namespaces or middleware. No need to define attribute for each route.

In below example we are 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 tow ways one in Implicit binding and another one is Explicit Binding.

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

Conclusion:

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

Leave a Comment