Set up a Mezzio handler
A handler turns a request into a response. A factory builds it, and the configuration provider tells the container about the factory.
mezzio/src/App/Handler/HelloHandler.php
<?php
declare(strict_types=1);
namespace App\Handler;
use Laminas\Diactoros\Response\TextResponse;
use Override;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
/** @api */
final class HelloHandler implements RequestHandlerInterface
{
#[Override]
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new TextResponse('Hello');
}
}
mezzio/src/App/Handler/HelloHandlerFactory.php
<?php
declare(strict_types=1);
namespace App\Handler;
/** @api */
final class HelloHandlerFactory
{
public function __invoke(): HelloHandler
{
return new HelloHandler();
}
}
mezzio/src/App/HelloConfigProvider.php
<?php
declare(strict_types=1);
namespace App;
use App\Handler\HelloHandler;
use App\Handler\HelloHandlerFactory;
/** @api */
final class HelloConfigProvider
{
/** @return array<string, mixed> */
public function __invoke(): array
{
return [
'dependencies' => [
'factories' => [
HelloHandler::class => HelloHandlerFactory::class,
],
],
];
}
}
Then register a route. More in the Mezzio documentation.