Event Based Request Dispatch
This module provides a way to deligate handling of requests to multiple listeners., (*1)
This has the effect of making your controllers thin and your code DRY and testable, (*2)
A sample installation utilizing the module can be found here, (*3)
Sample Application, (*4)
This module requires a laravel installation., (*5)
Installation
You can use composer to install, (*6)
composer require chencha/dispatcher
Basic Usage
An assumption is made that your application utilizes PSR-4 Autoloading, (*7)
Directory structure
.
โโโ Sample
โโโ Commands
โย ย โโโ SaveUser.php
โโโ Handlers
โย ย โโโ CommandHandler.php
โย ย โโโ RequestHandler.php
โโโ Models
โย ย โโโ Transactions.php
โโโ Requests
โโโ RetreiveUser.php
The handlers
The handlers are classes that register all classes that will respond to a request or a command. They must extend, (*8)
Chencha\Dispatcher\EventSubscriber;
The handler class must then provide the location of the commands of the commands or requests it will handle to the parent constructor., (*9)
Eg, (*10)
function __construct()
{
$path = "Sample.Commands";
parent::__construct($path);
}
The class has three methods of which only one is required. This are:, (*11)
- beforeListeners
- duringListeners (Required)
- afterListeners
- queuedListeners (Called outsite the request cycle. See http://laravel.com/docs/4.2/queues)
each of this methods must return an array if defined., (*12)
Eg, (*13)
/**
* @return array
*/
function duringListeners()
{
return [
Transactions::class
];
}
Subscribe the handlers
For the framework to be aware of your registered classes. You need to register your handlers., (*14)
This is bootstrap work and should be done in either app/start/global.php file or whereever you normally register listeners., (*15)
A sample declaration, (*16)
Event::subscribe(new \Sample\Handlers\CommandHandler());
Usage
In your controllers use the trait, (*17)
use \Chencha\Dispatcher\RequestDispatcher;
Now to run the request, (*18)
function getSaveuser()
{
$command = new \Sample\Commands\SaveUser(rand(1, 5));
$this->runRequest($command);
return "Success";
}
In this way all classes registered in the command handler will be called., (*19)
Since objects are usually passed by reference. Changes are made directly to the command object., (*20)
This is useful in a request where a response is needed, (*21)
Eg, (*22)
function getUser()
{
$request = new \Sample\Requests\RetreiveUser(rand(1, 5));
$this->runRequest($request);
return $request->response;
}
In this way you can populate say the response public property with all needed values for the response., (*23)
Gotchas
Nesting Level
If you register a lot of classes you are likely to run into this error, (*24)
PHP Error: Maximum function nesting level of '100' reached, aborting
This is because of how the laravel event dispatcher works., (*25)
To sort this problem simply add the line, (*26)
xdebug.max_nesting_level = 200
to /etc/php5/fpm/conf.d/20-xdebug.ini, (*27)
The higher the max nesting level the more classes you can register., (*28)
Serialization of closure
Your objects should be as simple as possible, preferably native php types., (*29)
At all costs avoid closures as they can not be serialized., (*30)