Laravel Routing

In this tutorial you will learn about the Laravel Routing and its application with practical example.

What is Laravel routing?

Routing is one of the core components of Laravel framework, it is simply a mechanism that performs the mapping for your requests to a specific controller action. All Laravel routes are defined in the file located as app/Http/routes.php file, which is automatically loaded by the framework.

Basic Routing

A very basic Laravel routes is simply expressed as following-

It accept two parameters URL and Closure (Closure is basically a anonymous function can be passed as a parameter to other functions).

These are the simplest routes defined as they really don’t require you pass to anything other than corresponding view. You need not to login or pass any parameters in their route definition.

Basic routing mostly used to display static pages in our site. Suppose you have some static pages as Home, About Us, Services and a Contact page in your site, and you want them to be routed as following –

Home page:
http://localhost:8000/

About Us page:
http://localhost:8000/about

Services page:
http://localhost:8000/services

Contact page:
http://localhost:8000/contact

All our static pages (Home, About Us, Services and Contact) will be routed as following in our app/Http/routes.php file.

 

Route Parameters

Sometime you may want to pass some parameters along with the URL, it can be either required or optional to pass parameters. There are two ways to pass the parameters along with the URL –

Required Parameters

As per this route definition, it is mandatory to pass the specified parameters in the URL. For example, if you want ID must be passed within the URL to perform some action based on that ID. Route definition for this will be as following –

Optional Parameters

As per this route definition, it is optional to pass the specified parameters in the URL. Optional parameters can be indicated by placing a ? mark after the parameter name, make sure to set default value for the corresponding parameter. Route definition with optional parameter will be as following –

As per the above route definition it is optional to pass “name“, default value for “name” is “Mark“.

Regular Expression Constraints

Sometime you may want to constrain or specify your route parameter’s value, it can be simply done using the where method. The where method simply accepts two parameters that is the name of the parameter and a regular expression to match parameter’s value.

Let’s have look on following route definitions –

Global Constraints

If you want a route parameter to be constrained by a given regular expression globally for all route definitions. It can be defined using pattern method in the boot method of your RouteServiceProvider.

For example if you want route parameter id to be numeric for all route definitions, it can done as following –

Named Routes

Named routes gives you a way to assign easy to remember alias/name for URLs. It can be simply using the as key while creating route definition.

Named route definition for controller actions –

Alternatively, you can define it using chain the name method at the end of the route definition –

Accessing Named Routes

Named routes can be access in your controller, view or anywhere in application as following –

Route Groups

Route groups gives you way to group a set of routes (Routes sharing common route attributes such as namespaces or middleware) in single route definition instead of defining those attributes individually.

Assign Middleware

Assign Namespaces

Route Prefixes

Route Prefixes allows you to define prefix for each route in a group by using prefix group array attribute. Suppose you are creating a CMS where you have a admin panel, you would prefer to have admin as prefix for set of routes belongs to admin panel. Then it can defined as following –

Router Methods for other HTTP Requests

Route::post(‘url’, ‘closure’) :- It is used to create an Item
Route::put(‘url’, ‘closure’) :- It is used to update an Item
Route::delete(‘url’, ‘closure’) :- It is used to delete an Item
Route::match(‘[‘get’, ‘post’]’,’url’, ‘closure’) :- It is used to define a route that responds to more than one type of HTTP requests.
Route::any(‘url’, ‘closure’) :- This responds to all types of HTTP requests.

In this tutorial we have learn about the Laravel Routing and its application with practical example. I hope you will like this tutorial.