What is a Laravel Route?

Thu, Jun 10, 2021

Read in 3 minutes

In this bite, we will have a taste of the Laravel Route in all its glory.

What is a Laravel Route?

Introduction

A Route in Laravel is the first point of interaction with your application from the web (or API) end.

At the most basic level, the Route connects requests sent to your application to the relevant section of your application as determined by your rules as the application developer. You can think of it as the “doorman” that also doubles up as an usher. However, our doorman is much more powerful than just opening doors and showing the way. This doorman can have some powerful friends that add even more security to your application. Something more like a concierge, if you like.

We will get to these in the future.

OK. Tell me more

The basic form of a Route is a function in a class that takes two parameters

There are other forms which are simpler/more complex forms of the Laravel Route which we will discuss in the future. We will keep those aside for now.

The route effectively checks the request, confirm its method (POST, GET, DELETE, PATCH, etc.) and decides if the request can be sent to its intended destination or not. If there is no route defined matching the incoming request, that request is denied and an appropriate error is thrown.

An illustration describing a router Illustration by Anna Golde from iocns8.com

The Route in Laravel is easily accessed by the Route facade which enables us to call the methods of the class statically. You know, Object::function() and you don’t have to instantiate it rather than instantiating it and then calling Object->function().

If the word facade confuses you, don’t worry, we will cover it in some future bites. But for now just know that its a Laravel concept that enables non-static methods to be called statically (“static proxies” - from the Laravel documentation).

Examples

In the code samples below, we “use” the Illuminate\Support\Facades\Route facade in our code.

Route with a closure

Route closure example An example signature of a closure based route.

In the above code sample we have the basic signature of a route method that takes two parameters:

Route closure implementation An implementation of a closure based route.

In the above code sample we have the implementation for the previous example.

In this example the route method is a “GET” request and our “doorman” will only respond to get requests and nothing else.

Route with an array

Route array example An example of an array based route.

In the above code sample we have the basic signature of a route method that takes two parameters:

Route array post implementation An implementation of an array based route with a post method.

In the above code sample we have an implementation for the previous example.

In this example the route method is a “POST” request and our “doorman” will only respond to post requests and nothing else.

Next…

In the next bite we will delve deeper into the “Route” and its other forms.