PHP code example of chimera / routing

1. Go to this page and download the library: Download chimera/routing 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/ */

    

chimera / routing example snippets



declare(strict_types=1);

use Chimera\Routing\RouteParamsExtraction;
use Lcobucci\ContentNegotiation\ContentTypeMiddleware;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Application;
use Zend\Expressive\Handler\NotFoundHandler;
use Zend\Expressive\Helper\BodyParams;
use Zend\Expressive\Helper\ServerUrlMiddleware;
use Zend\Expressive\Helper\UrlHelperMiddleware;
use Zend\Expressive\MiddlewareFactory;
use Zend\Expressive\Router\Middleware\DispatchMiddleware;
use Zend\Expressive\Router\Middleware\ImplicitHeadMiddleware;
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware;
use Zend\Expressive\Router\Middleware\MethodNotAllowedMiddleware;
use Zend\Expressive\Router\Middleware\RouteMiddleware;
use Zend\Stratigility\Middleware\ErrorHandler;

return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container) : void {
    $app->pipe(ErrorHandler::class);
    $app->pipe(ServerUrlMiddleware::class);

    // Handles content negotiation, ensuring that the response format is the best one
    // according to what was requested via the `Accept` header
    $app->pipe(ContentTypeMiddleware::class);

    $app->pipe(RouteMiddleware::class);

    // Puts the matched arguments in a standard place (must be executed after the
    // `Zend\Expressive\Router\Middleware\RouteMiddleware` middleware, otherwise
    // matched routed info is not available)
    $app->pipe(RouteParamsExtraction::class);

    // It's quite important to add this one to the list, so that we ensure
    // that the request body is properly parsed and can be retrieved via
    // `ServerRequestInterface#getParsedBody()` - used by the `HttpRequest` input
    $app->pipe(BodyParams::class);

    $app->pipe(ImplicitHeadMiddleware::class);
    $app->pipe(ImplicitOptionsMiddleware::class);
    $app->pipe(MethodNotAllowedMiddleware::class);
    $app->pipe(UrlHelperMiddleware::class);
    $app->pipe(DispatchMiddleware::class);
    $app->pipe(NotFoundHandler::class);
};



declare(strict_types=1);

use Psr\Container\ContainerInterface;
use Zend\Expressive\Application;
use Zend\Expressive\MiddlewareFactory;

/**
 * Considering you have the following services in your DI container:
 *
 * - `album.list` => new FetchOnly(
 *     new ExecuteQuery(**query bus**, **message creation strategy**, MyApi\FetchAlbumList::class),
 *     ResponseInterface::class
 * );
 * - `album.find_one` => new FetchOnly(
 *     new ExecuteQuery(**query bus**, **message creation strategy**, MyApi\FindAlbum::class),
 *     ResponseInterface::class
 * );
 * - `album.create` => new CreateOnly(
 *     new ExecuteCommand(**command bus**, **message creation strategy**, MyApi\CreateAlbum::class),
 *     ResponseInterface::class,
 *     'album.find_one',
 *     UriGenerator::class,
 *     IdentifierGenerator::class,
 *     201
 * );
 * - `album.update_album` => new ExecuteAndFetch(
 *     new ExecuteCommand(**command bus**, **message creation strategy**, MyApi\UpdateAlbum::class),
 *     new ExecuteQuery(**query bus**, **message creation strategy**, MyApi\FindAlbum::class),
 *     ResponseInterface::class
 * );
 */
return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container) : void {
    $app->get('/album', 'album.list', 'album.list');
    $app->post('/album', 'album.create', 'album.create');
    $app->get('/album/{id}', 'album.find_one', 'album.find_one');
    $app->patch('/album/{id}', 'album.update_album', 'album.update_album');
};