An Introduction To Routing In Laravel 5.4

An Introduction To Routing In Laravel 5.4

In this article, I will discuss the concept of routing in Laravel 5.4.

What Is a Route?

Route is a way of creating a request URL of your application. These URL do not have to map to specific files on  website. The best thing about these URL is that they are both human readable and SEO friendly.
In Laravel 5.4, routes are created inside the routes folder. Routes for the website are created in web.php file, Similarly, the routes for the API are created inside api.php.
The default installation of Laravel 5.4 comes with two routes, one for the web and the other for API. Here is how the route for web in web.php looks like:
In the above code, a route is defined for the homepage. Every time this route receives a get request for /, It will return the view welcome. Views are the frontend of the application and I will  discuss the topic in an upcoming installment of this series.
The structure of route is very simple. Open the appropriate file (either web.php or api.php) and start the code with Route:: This is followed by the request you want to assign to that specific route and then comes the function that will be executed as a result of the request.
Laravel offers the following route methods:
  • get
  • post
  • put
  • delete
  • patch
  • options

Create Laravel Routes

I will now discuss how you could create your own routes in Laravel 5.4.
Note: All the post, put and delete requests require CSRF token to be sent along with the request. Otherwise the request will be rejected. Learn more about CSRF protection in Laravel official docs.

A Basic GET Route

I will now create a basic route that will print the table of 2.
In the above code, I created a GET request route for /table,  which will print the table of 2 on the screen.
Now, what if I want the user to decide the number for which the route will print the table. Here is the code for such a route:
In the above code, I defined a route parameter and passed it to the route function. Now, whenever a user route to /table and add a number to the request (for instance, /table/3), the function will print the table of that number to the screen.Right now, I have created two separate routes (one that output the table of 2 and the other that output the table for a user defined number). What if there is a way of combining the functionality of both the routes in a single route?
Indeed! Laravel offers the functionality of Optional Parameters through I could add optional parameters by adding ? after the optional parameter and the default value. I will now do this for the above mentioned example route:
What we have done in the above code is that we have made our route parameter i.e. number optional so if a user route /table only then table of 2 will be generated by default and if a user route to /table/{number} then the table of that number will be printed.

Regular Expressions Constraints For Route Parameters

Above we have created a route for table, but how to assure that if route parameter is really a number to be safe from getting error while generating a table? In Laravel you can define a constraint for your route parameter by using where method on route instance. The wheremethod takes parameter name and a regular expression rule for that parameter. Let’s now define a constraint for our {number} parameter to assure that only number is passed to the function.
In the above code snippet, I defined a regular expression for our route’s number. Now if a user try to route to /table/no, a NotFoundHttpException will be displayed.

Routing with Controller Function

In Laravel, you can define a Controller method to a route. In the presence of a controller method, whenever a user comes to the route, the actions will be perform by the method of the controller that has been defined.
To assign a controller method to the route, use the following code snippet:
The code starts with Route:: and then define the request method for the route. Next, define your route and then define the Controller along with the method that you want to bind to this route by adding the @ symbol before the method’s name.

Naming Your Route

In Laravel, you can define the name to your route. This name often proves very useful in various scenarios. For example if you want to redirect a user from one route to another, you do not need to define the full redirect URL. Rather, you can just give its name and the code will work! You can define the name of the route by using the name method in the route instance.
Now, I could re-generate the URL for this route through:
Similarly, for redirecting to this URL, the proper syntax would be:

Conclusion

In this installment of the series, I discussed routing in Laravel 5.4. I described various related concepts regarding routes such as regular expressions and names for the routes. To learn more about routing, you could also refer to the official Laravel routing documentation.

Comments