All about Laravel Routing

Table of Contents

  • What are Routes?
  • Laravel Route Directory Setup
  • HTTP Request Methods and Available Router Methods
  • Basic Routing
  • Redirect and View Routes
  • Route Parameters and Named Routes
  • Route Groups
  • Conclusion

What are Routes?

Routes are the access URL for the different pages on your web application. Think about it like the different departments in an organization, they all have a department name, and are found via these names. The same applies for your application they can be found on your web client via their routes.

Laravel Route Directory Setup

The Laravel Routes are created in the Routes directory which is automatically loaded by the framework, this directory consists of two route files:

1. The routes/web.php file which contains routes that can be accessed in your browser are assigned to the web middleware which provides certain features such as CRSF protection and session.
2. The routes/api.php file which contains routes for APIs are stateless and belong to the API middleware.

HTTP Request Methods and Available Router Methods

HTTP defines a set of request methods to indicate the desired action to be performed for a given resource, a Laravel Route is usually accompanied by request method. Although they can also be nouns, these request methods are sometimes referred as HTTP verbs. These methods includes Get, Post, Head, Put, Delete, Patch, Options.

Available Router Methods
The router allows you to register routes that respond to any HTTP verb:

Laravel makes using multiple HTTP verbs easy, and this can be done using the match method which allows you use multiple verbs or any method which allows you use any HTTP verb.

Basic Routing

The simplest form of a Laravel Route contains a URI and a closure.

A closure is an anonymous function written in an object oriented way or an object representation of an anonymous function.

Another form of a Laravel Route contains a URI and a controller name and function name.

Redirect and View Routes

When dealing with multiple pages, there will be a need to go from one page to another, this is made possible by the Laravel redirect method.

The View Route returns a view and accepts a URI as its first argument and a view name as named in the views folder in the resource directory as it’s second argument, the third argument is optional and allows you to pass an array of data.

Route Parameters and Named Routes

Route parameters are usually enclosed in curly braces {}, sometimes you will need to take parts of the URI within your route.

Route Parameters can be made optional, this is done by placing a ? mark after the parameter and giving the route’s variable a default value.

Regular Expressions can also help to choose a particular pattern in which your routes should follow using the where method, which accepts the name of the parameter and a regular expression defining how the parameter should be constrained.

Naming Routes is a way of assigning alias to routes using the name method.

Route Groups

Grouping routes is a way of classifying routes and giving them specific attributes .i.e. only routes within that group have that attribute. Shared attributes are specified in an array format as the first parameter to the Route::group method. These attributes include middleware and namespace.

Assigning middleware to a route group:

Prefixes can also be specified for routes in a particular group

Conclusion

In conclusion, creating routes is usually a straight forward activity, which Laravel has made even easier by the provision of different methods that allow manipulating of routes.

Related Posts