1. Go to this page and download the library: Download morphable/micro library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
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;
});
// : 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
})
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
$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