pine3ree / p3-mezzio-controller
A middleware wrapper for controller-like classes for mezzio/mezzio
Requires
- php: ^7.1
- mezzio/mezzio: ^3.0
- psr/container: ^1.0
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- laminas/laminas-diactoros: ^2.2
- malukenho/docheader: ^0.1
- phpspec/prophecy: ^1.10
- phpstan/phpstan: ^0.12
- phpstan/phpstan-strict-rules: ^0.12
- phpunit/phpunit: ^7.5 || ^8.5
- squizlabs/php_codesniffer: ^3.5
README
A middleware wrapper for controller-like classes for mezzio/mezzio
STATUS
WIP!
Installation
You can install this library using Composer (with "minimum-stability": "dev"):
$ composer require pine3ree/p3-mezzio-controller
Documentation
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.
TODO:
- 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.