chubbyphp/chubbyphp-framework-router-sunrise

此包已被废弃,不再维护。作者建议使用 chubbyphp/chubbyphp-framework-router-fastroute 包。

为 chubbyphp-framework 实现的 sunrise 路由器。

2.0.0 2022-10-02 19:38 UTC

This package is auto-updated.

Last update: 2023-12-02 17:58:31 UTC


README

CI Coverage Status Infection MSI Latest Stable Version Total Downloads Monthly Downloads

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

描述

Sunrise Router 实现用于 chubbyphp-framework

已弃用:建议使用 chubbyphp-framework-router-fastroute

要求

安装

通过 Composerchubbyphp/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