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:

Request routing

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

Request routing

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:


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


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.