Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

An Introduction to Routing in Laravel 5.5

January 18, 2018

4 Min Read
Reading Time: 4 minutes

In my previous article on Laravel, I presented a basic introduction to Laravel 5.5. In this article, I will discuss the concept of routing in Laravel 5.5.

Routing In Laravel 5.5

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 a website. The best thing about these URL is that they are both human readable and SEO friendly.

In Laravel 5.5, 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.5 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:

Route::get('/', function () {

   return view('welcome');


Prevent Your Laravel Applications From Being Hacked

Deploy Your Laravel Apps With Us & Experience The Solidified Security Features

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.5.

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.

Route::get('/table', function () {

   for($i =1; $i <= 10 ; $i++){

       echo "$i * 2 = ". $i*2 ."<br>";



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:

Route::get('/table/{number}', function ($number) {

   for($i =1; $i <= 10 ; $i++){

       echo "$i * $number = ". $i* $number ."<br>";



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:

Route::get('/table/{number?}', function ($number = 2) {

   for($i =1; $i <= 10 ; $i++){

       echo "$i * $number = ". $i* $number ."<br>";



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 `where` method 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.

Route::get('/table/{number?}', function ($number = 2) {

   for($i =1; $i <= 10 ; $i++){

       echo "$i * $number = ". $i* $number ."<br>";


})->where('number', '[0-9]+');

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.

Laravel 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:

Route::get('/home', 'YourController@functionname');

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.

Route::get('/table/{number?}', function ($number = 2) {

   for($i =1; $i <= 10 ; $i++){

       echo "$i * $number = ". $i* $number ."<br>";


})->where('number', '[0-9]+')->name(‘table’);

Now, I could re-generate the URL for this route through:

$url = route('table');

Similarly, for redirecting to this URL, the proper syntax would be:

return redirect()->route('table');


In this installment of the series, I discussed routing in Laravel 5.5. 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.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Launch Laravel apps with 1-Click Install. Painless Server Management

Deploy your apps on optimized Laravel hosting servers.

Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

BFCM 2019