morphable / micro
micro framework
v1.0.1
2019-11-29 17:41 UTC
Requires
- php: ^7.1
- psr/container: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- php: ^7.2
- league/container: ^3.3
- nyholm/psr7: ^1.2
- nyholm/psr7-server: ^0.4.1
- phpunit/phpunit: >=5.4.3
- squizlabs/php_codesniffer: ^2.3
This package is auto-updated.
Last update: 2024-12-22 00:04:15 UTC
README
a micro framework based on the psr-15 and psr-7 standard
Simple usage
<?php use \Morphable\Micro; use \Psr\Http\Message\ServerRequestInterface; $micro = new Micro(); $router = $micro->routing(); $router->add('GET', '/user/:id', function (ServerRequestInterface $request, array $args) { $id = $args['id']; /** @link https://www.php-fig.org/psr/psr-7/ */ return $response; }); try { $response = $micro->handle($request); // \Psr\Http\Message\ResponseInterface } catch (\Exception $e) { // 404 }
Callbacks
<?php use \Psr\Http\Message\ServerRequestInterface; // specific method in controller $router->add('GET', '/user/:id', ['controller', 'method']); // __invoke $router->add('GET', '/user/:id', 'controller'); // static method $router->add('GET', '/user/:id', [controller::class, 'method']); function callback(ServerRequestInterface $request, array $args) { return $response; } // function $router->add('GET', '/user/:id', 'callback'); // annonymous function $router->add('GET', '/user/:id', function (ServerRequestInterface $request, array $args) { return $response; });
Route pattern
<?php // : is mandatory argument ?: is optional argument $router->add('GET', '/user/:userId/profile', function ($request, $args){ $args['userId'] // second parameter in url }); $router->add('GET', '/callback/?:channel', function ($request, $args) { $args['channel'] // second parameter or null })
Middleware
<?php use \Psr\Http\Message\ResponseInterface; use \Psr\Http\Message\ServerRequestInterface; use \Psr\Http\Server\MiddlewareInterface; /** @link https://www.php-fig.org/psr/psr-15/ */ class Middleware implements MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { // return ResponseInterface or handle next request return $handler->handle($request); } } $router->add('GET', '/user/:id', ['controller', 'method']) ->middleware('middleware'); // from container
Groups
<?php $router->group('api', function ($router) { // prefix of api $router->add('GET', '/', ['controller', 'method']); // pattern: /api $router->group('user', function ($router) { $router->add('GET', '/:id', ['controller', 'method']); // pattern: /api/user/:id })->middleware('middleware'); })->middleware('middleware'); // counts for every route inside
After call
<?php $router->add('GET', '/', ['controller', 'method']) ->after(function ($reqest, $args, $response): void {}) ->after(['class', 'method']) ->after('function');