A middleware wrapper for controller-like classes for mezzio/mezzio
WIP!
You can install this library using Composer (with "minimum-stability": "dev"):
$ composer require pine3ree/p3-mezzio-controller
You can now define 2 new types of route handled by generic-class controller methods:
Define a route using the callable-string format:
$app->get('/home/kitchen', 'App\Controller\Home::kitchen', 'home/kitchen');
Define a route using the callable-array format:
use App\Controller\Home;
//...
$app->get('/home/bedroom', [Home::class, 'bedroom'], 'home/bedroom');
Define routes using a configuration file:
// config/autoload/routes.global.php
use App\Controller\Home;
use App\Middleware\BeforeHomeControllerMiddleware;
use App\Middleware\AfterHomeControllerMiddleware;
return [
'routes' => [
'home/kitchen' => [
'path' => '/home/kitchen',
'middleware' => 'App\Controller\Home::kitchen',
],
'home/bedroom' => [
'path' => '/home/kitchen',
'middleware' => [Home::class, 'bedroom'],
],
'home/living-room' => [
'path' => '/home/living-room',
'middleware' => [
BeforeHomeControllerMiddleware::class,
[Home::class, 'livingRoom'],
AfterHomeControllerMiddleware::class,
],
],
],
];
These definitions works only on the condition that if the controller class exists and the target method is public.
As of now the target controller-method signature may be one of the following:
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
class Home
{
public function kitchen(ServerRequestInterface $request): ResponseInterface
{
// return a response
}
public function bedroom(): ResponseInterface
{
// return a response
}
}
The controller class may be a simple contructor-less class. In most of cases it will have dependencies so it muste me defined in the container configuration along with its factory.
- Add strategy to pass request attributes as arguments into the target controller-method
- Add strategy to allow null, string, array, xml, etc... return values from the controller-methods and build appropriate response based on the return type.