Download the PHP package think-codee/laravel-command-bus without Composer
On this page you can find all versions of the php package think-codee/laravel-command-bus. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-command-bus
Laravel Command Bus
This package provides a simple and efficient way to implement the command pattern in your Laravel applications, allowing for better separation of concerns and more maintainable code.
Installation
Install with composer:
Optionally, you can publish the config file with:
Below is the configuration file content:
Usage
This package allows you to create and manage multiple command buses, providing a robust and flexible system for handling different types of commands within your Laravel application. It is especially useful when implementing architectural patterns like CQRS (Command and Query Responsibility Segregation), which separates read and update operations. Each bus can be configured with its own set of middlewares and handler resolvers, enhancing the modularity and organization of your application.
Defining buses
To create a bus, start by defining an interface:
Next, create a class that implements this interface:
You may use the following artisan command to create a bus:
Each bus must be registered in the configuration file. The package will bind the interface to the corresponding class and assign it an alias:
You can then use the bus in your application as follows:
Defining commands and handlers
To define a command:
To define a handler for the command:
You may use the following artisan command to create a command with handler:
Defining middleware
In this package, middleware works similarly to standard Laravel middlewares. They allow you to run code before or after a command is handled, providing a way to add common behavior to your command handling logic.
To define a middleware, create a class that processes the command before and/or after it is handled:
Registering middleware
You can register middleware to specific command handlers using attributes:
- use the
#[Middleware]
attribute to add middleware to a handler. - the second parameter of the attribute is true to prepend the middleware or false (or omitted) to append it.
- use the
#[ResetMiddleware]
attribute to remove all existing middleware for that command (registered in config) and apply only the specified middleware.
You can still register middlewares in the configuration file, assigning them to specific buses. The middleware will be applied to all commands processed by the bus.
Handler resolvers
Each bus has a handler resolver that determines how to resolve the appropriate handler for a given command. The resolver and the method used to handle the command are specified in the configuration file for each bus:
Every handler resolver must implement the HandlerResolver
interface. The SuffixHandlerResolver
comes by default with the package. If you have a command named TestCommand.php
, it will try to resolve the handler name as TestCommandHandler.php
in the same directory.
When a command is run through the resolved handler, it will try to use the method specified in the configuration file.
Specifying a Handler for a Specific Command
You can also set a specific handler for a command using the #[Handler]
attribute. The second parameter is optional and specifies the method name to be used. If not specified, the method name from the configuration file will be used.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Running Tests
To run tests, run the following command