Sunrise Router implementation for chubbyphp-framework.
DEPRECATED: I suggest to use chubbyphp-framework-router-fastroute.
- php: ^8.0
- chubbyphp/chubbyphp-framework: ^5.0.3
- chubbyphp/chubbyphp-http-exception: ^1.0.1
- psr/http-message: ^1.0.1
- sunrise/http-router: ^2.6
Through Composer as chubbyphp/chubbyphp-framework-router-sunrise.
composer require chubbyphp/chubbyphp-framework-router-sunrise "^2.0"
<?php
declare(strict_types=1);
namespace App;
use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\Middleware\ExceptionMiddleware;
use Chubbyphp\Framework\Middleware\RouterMiddleware;
use Chubbyphp\Framework\RequestHandler\CallbackRequestHandler;
use Chubbyphp\Framework\Router\Sunrise\Router;
use Chubbyphp\Framework\Router\Route;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Psr7\Factory\ResponseFactory;
use Slim\Psr7\Factory\ServerRequestFactory;
$loader = require __DIR__.'/vendor/autoload.php';
$responseFactory = new ResponseFactory();
$app = new Application([
new ExceptionMiddleware($responseFactory, true),
new RouterMiddleware(new SunriseRouter([
Route::get('/hello/{name<[a-z]+>}', 'hello', new CallbackRequestHandler(
function (ServerRequestInterface $request) use ($responseFactory) {
$name = $request->getAttribute('name');
$response = $responseFactory->createResponse();
$response->getBody()->write(sprintf('Hello, %s', $name));
return $response;
}
))
])),
]);
$app->emit($app->handle((new ServerRequestFactory())->createFromGlobals()));
2023 Dominik Zogg