Request routing in laravel 5.1

Laravel 5 has adopted the command bus pattern. Laravel 4 viewed commands as something to be executed from the command line, whereas in Laravel 5, a command can be used in any context, allowing excellent reuse of code.

The following is an example of the Laravel 4 HTTP request flow:

Here is an example of the Laravel 5 HTTP request flow:

The first image illustrates the Laravel 4 request flow. The request via HTTP was handled by the router, and then sent to the controller, where generally, we could then talk to either the repository or directory of the model. In Laravel 5, this is still possible; however, as shown in the second image, we can see that the ability to add additional blocks, layers, or modules allows us to separate the life cycle of the request into individual isolated pieces. Laravel 4 allowed us to put all of our code to handle the request inside the controller, while in Laravel 5, we are free to do the same, although now we are also able to easily separate the request into various pieces. Some of these concepts are derived from Domain-driven Design (DDD).

Inside the controller, the command is instantiated using the Data Transfer Object (DTO) paradigm. Then, the command is sent to the command bus, where it is handled by a handler class, which has two methods: __construct() and handle(). Inside the handler, we fire or instantiate an event. The event is likewise handled in the same way by an event handler method with two methods: __construct() and handle().

The directory structure is very clean and looks like this:

/app/Commands
/app/Events/
/app/Handlers/
/app/Handlers/Commands
/app/Handlers/Events
/app/HTTP/Controllers

It’s rather self-explanatory; the commands and events are in their respective directories, while the handlers for each have their own directories.

Note

Laravel 5.1 has changed the name of the app/Commands directory to app/Jobs to ensure that programmers do not get the concepts of the command bus and console commands mixed up.

Deven Rathore

Deven is an Entrepreneur, and Full-stack developer, Constantly learning and experiencing new things. He currently runs CodeSource.io and Dunebook.com.

Published by
Deven Rathore

Recent Posts

Neural Networks for Creating Blog Texts

The world is progressing at unprecedented rates at the current moment, especially in terms of…

3 hours ago

Top 20 Opensource Python Tkinter Projects

This article will highlight the Top 20 Opensource Python Tkinter Projects which we believe will…

2 days ago

Beginners guide to Sneaker Proxies

With their numerous applications in streamlining the data flow, securing both the servers and the…

5 days ago

Top 20 Node.js dashboard templates

In this article, We will be looking at some of the top Node.js dashboard templates.…

7 days ago

Tips on How to Conduct an Interview When Hiring a Software engineering Company

In case you want to hire a software engineering company to bring your solution to…

7 days ago

Top 5 Advantages Of Using Node.js As Your Web Development Technology

Did you know that JavaScript remains to be the most used programming language? A survey…

1 week ago