Routes

Routes are configured within the application from the /config/routes.php file. For private routes that require authentication, App\Middleware\JwtAuthenticationMiddleware is used.

declare(strict_types=1);

use Mezzio\Application;
use Mezzio\MiddlewareFactory;
use App\Middleware\JwtAuthenticationMiddleware;
use Psr\Container\ContainerInterface;

return static function (Application $app, MiddlewareFactory $factory, ContainerInterface $container) : void {

    // public
    $app->route('/api/auth/token', App\Handler\Api\AuthHandler::class, ['POST']);
    $app->route('/api/auth/refresh', [App\Handler\Api\AuthHandler::class], ['POST']);

    // private
    $app->route('/api/auth/changePassword', [JwtAuthenticationMiddleware::class, App\Handler\Api\AuthHandler::class], ['PUT']);
    $app->route('/api/auth/logout', [JwtAuthenticationMiddleware::class, App\Handler\Api\AuthHandler::class], ['GET']);
}

Another private route example;

return static function (Application $app, MiddlewareFactory $factory, ContainerInterface $container) : void {

    // private
    $companies = [
        JwtAuthenticationMiddleware::class,
        Mezzio\Authorization\AuthorizationMiddleware::class,
        App\Handler\Api\CompanyHandler::class
    ];
    $app->route('/api/companies/create', $companies, ['POST']);
    $app->route('/api/companies/update/:companyId', $companies, ['PUT']);
    $app->route('/api/companies/delete/:companyId', $companies, ['DELETE']);
    $app->route('/api/companies/findAll', $companies, ['GET']);
    $app->route('/api/companies/findAllByPaging',$companies, ['GET']);
    $app->route('/api/companies/findOneById/:companyId', $companies, ['GET']);
}

Route Options

$apiHandler = [
    App\Middleware\SetLocaleMiddleware::class,
    App\Middleware\VersionValidateMiddleware::class,
    App\Handler\ApiIndexHandler::class
];
$app->route('/api/:version/.*', $apiHandler, ['GET'], 'doc_index')
    ->setOptions(
    [
        'constraints' => [
            'version' => '\d.{1,4}',
        ],
    ]
);

For more detailed information about the routes, see the link Laminas Router.

An Example

To test whether the /api/:version/.* route shown above works or not, let's create a middleware named RouteTestMiddleware in the App\Middleware\ folder.

declare(strict_types=1);

namespace App\Middleware;

use Mezzio\Router\RouterInterface;
use Mezzio\Router\Result as RouteResult;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\I18n\Translator\TranslatorInterface;

class RouteTestMiddleware implements MiddlewareInterface
{
    protected $router;

    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
    {
        $routeResult = $request->getAttribute(RouteResult::class);
        $params = $routeResult->getMatchedParams();
        print_r($params);
        return $handler->handle($request);
    }
}

We must define this middleware, which we created for testing purposes, in the pipeline.php file as follows.

config/pipeline.php

use App\Middleware\RouteTestMiddleware;

// Register your middleware in the middleware pipeline.
$app->pipe(RouteTestMiddleware::class);

Test the middleware by visiting the route address.

http://demo.local/api/1.0/index.html
This documents is available for subscribers only

Get Full Accesss