Popularity
1.9
Growing
Activity
6.6
-
74
4
5

Description

A based PSR-15 microframework that also sets maximum flexibility with minimum complexity and easy replaceability of the individual components, but also of the framework. Although performance was not a focus, it's currently the fastest PSR-15 based framework (php-fpm) on the market.

Monthly Downloads: 761
Programming language: PHP
License: MIT License

chubbyphp-framework alternatives and similar libraries

Based on the "API" category.
Alternatively, view chubbyphp-framework alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of chubbyphp-framework or a related project?

Add another 'API' Library

README

chubbyphp-framework

Build Status Coverage Status Infection MSI Latest Stable Version Total Downloads Monthly Downloads Daily Downloads

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

A based PSR-15 microframework that also sets maximum flexibility with minimum complexity and easy replaceability of the individual components, but also of the framework. Although performance was not a focus, it's currently the fastest PSR-15 based framework (php-fpm) on the market.

[Application workflow](doc/Resources/workflow.png?raw=true "Application workflow")

Requirements

Suggest

Router

Any Router which implements Chubbyphp\Framework\Router\RouterInterface can be used.

PSR 7 / PSR 17

Installation

Through Composer as chubbyphp/chubbyphp-framework.

Aura.Router

composer require chubbyphp/chubbyphp-framework "^3.1" \
    chubbyphp/chubbyphp-framework-router-aura "^1.0" \
    slim/psr7 "^1.0"

Example

FastRoute

composer require chubbyphp/chubbyphp-framework "^3.1" \
    chubbyphp/chubbyphp-framework-router-fastroute "^1.0" \
    slim/psr7 "^1.0"

Example

SunriseRouter

composer require chubbyphp/chubbyphp-framework "^3.1" \
    chubbyphp/chubbyphp-framework-router-sunrise "^1.0" \
    slim/psr7 "^1.0"

Example

Symfony Routing

composer require chubbyphp/chubbyphp-framework "^3.1" \
    chubbyphp/chubbyphp-framework-router-symfony "^1.0" \
    slim/psr7 "^1.0"

Example

Usage

Fastroute

Basic

<?php

declare(strict_types=1);

namespace App;

use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\ErrorHandler;
use Chubbyphp\Framework\Middleware\ExceptionMiddleware;
use Chubbyphp\Framework\Middleware\RouterMiddleware;
use Chubbyphp\Framework\RequestHandler\CallbackRequestHandler;
use Chubbyphp\Framework\Router\FastRoute\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';

set_error_handler([new ErrorHandler(), 'errorToException']);

$responseFactory = new ResponseFactory();

$app = new Application([
    new ExceptionMiddleware($responseFactory, true),
    new RouterMiddleware(new Router([
        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;
            }
        ))
    ]), $responseFactory),
]);

$app->emit($app->handle((new ServerRequestFactory())->createFromGlobals()));

Advanced

This is an example of middleware(s) before and after the routing was done.

If you need to be able to continue without finding a route, I recommend writing a RouterMiddleware that will pass either the route or the RouteException and at the end another middleware that will convert the RouteException to a http response.

<?php

declare(strict_types=1);

namespace App;

use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\ErrorHandler;
use Chubbyphp\Framework\Middleware\CallbackMiddleware;
use Chubbyphp\Framework\Middleware\ExceptionMiddleware;
use Chubbyphp\Framework\Middleware\RouterMiddleware;
use Chubbyphp\Framework\RequestHandler\CallbackRequestHandler;
use Chubbyphp\Framework\Router\FastRoute\Router;
use Chubbyphp\Framework\Router\Route;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Psr7\Factory\ResponseFactory;
use Slim\Psr7\Factory\ServerRequestFactory;

$loader = require __DIR__.'/vendor/autoload.php';

set_error_handler([new ErrorHandler(), 'errorToException']);

$responseFactory = new ResponseFactory();

$app = new Application([
    new ExceptionMiddleware($responseFactory, true),
    new CallbackMiddleware(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
        return $handler->handle($request);
    }),
    new RouterMiddleware(
        new Router([
            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;
                }
            ))
        ]),
        $responseFactory
    ),
    new CallbackMiddleware(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
        /** @var Route $route */
        $route = $request->getAttribute('route');

        if ('hello' === $route->getName()) {
            $request = $request->withAttribute('name', 'world');
        }

        return $handler->handle($request);
    }),
]);

$app->emit($app->handle((new ServerRequestFactory())->createFromGlobals()));

Emitter

  • [Emitter][65]

Middleware

  • [CallbackMiddleware][70]
  • [ExceptionMiddleware][71]
  • [LazyMiddleware][72]
  • [MiddlewareDispatcher][73]
  • [NewRelicRouteMiddleware][74]
  • [RouterMiddleware][75]

RequestHandler

  • [CallbackRequestHandler][80]
  • [LazyRequestHandler][81]

Router

  • [Group][90]
  • [Route][91]

Webserver

  • [Builtin (development only)][100]
  • [Nginx][101]

Skeleton

Application Server

ReactPHP

<?php

declare(strict_types=1);

namespace App;

use Chubbyphp\Framework\Application;
use React\EventLoop\Factory;
use React\Http\Server;
use React\Socket\Server as Socket;

/** @var Application $app*/
$app = ...;

$loop = Factory::create();

$socket = new Socket(8080, $loop);

$server = new Server($app);
$server->listen($socket);

$loop->run();

Roadrunner

<?php

namespace App;

use Chubbyphp\Framework\Application;
use Spiral\Goridge\StreamRelay;
use Spiral\RoadRunner\Worker;
use Spiral\RoadRunner\PSR7Client;

ini_set('display_errors', 'stderr');

/** @var Application $app */
$app = ...;

$worker = new Worker(new StreamRelay(STDIN, STDOUT));
$psr7 = new PSR7Client($worker);

while ($req = $psr7->acceptRequest()) {
    try {
        $psr7->respond($app->handle($req));
    } catch (\Throwable $e) {
        $psr7->getWorker()->error((string)$e);
    }
}

Swoole

Workerman

Migration

From 3.x to 4.x

Routing

Removed the following methods on group: group, route, middlewares, middleware, pathOptions, use additional parameters. Removed the following methods on route: middlewares, middleware, pathOptions, use additional parameters.

from
Group::create('/{id}')
    ->pathOptions(['tokens' => ['id' => '\d+']])
    ->middleware($middleware1)
    ->route(Route::get('/{slug}', 'element_read', $handler)
        ->pathOptions(['tokens' => ['slug' => '[a-z]+']])
        ->middleware($middleware2)
    )
    ->group(Group::create('/{slug}')
        ->pathOptions(['tokens' => ['slug' => '[a-z]+']])
        ->middlewares([$middleware2])
        ->route(Route::get('/{key}', 'another_route', $handler)
            ->pathOptions(['tokens' => ['key' => '[a-z]+']])
            ->middleware($middleware3)
        )
    )
    ->route(Route::get('/{slug}/{key}/{subKey}', 'yet_another_route', $handler)
        ->pathOptions(['tokens' => ['slug' => '[a-z]+', 'key' => '[a-z]+', 'subKey' => '[a-z]+']])
        ->middleware($middleware2)
    )
;
to
Group::create('/{id}', [
    Route::get(
        '/{slug}',
        'element_read',
        $handler,
        [$middleware2],
        ['tokens' => ['slug' => '[a-z]+']]
    ),
    Group::create('/{slug}', [
        Route::get(
            '/{key}',
            'another_route',
            $handler,
            [$middleware3],
            ['tokens' => ['key' => '[a-z]+']]
        ),
    ], [$middleware2], ['tokens' => ['slug' => '[a-z]+']]),
    Route::get(
        '/{slug}/{key}/{subKey}',
        'yet_another_route',
        $handler,
        [$middleware2],
        ['tokens' => ['slug' => '[a-z]+', 'key' => '[a-z]+', 'subKey' => '[a-z]+']]
    ),
], [$middleware1], ['tokens' => ['id' => '\d+']]);

From 2.x to 3.x

Aura.Router

  1. Upgrade: composer require chubbyphp/chubbyphp-framework "^3.1" chubbyphp/chubbyphp-framework-router-aura
  2. Replace Chubbyphp\Framework\Router\AuraRouter with Chubbyphp\Framework\Router\Aura\Router.

FastRoute

  1. Upgrade: composer require chubbyphp/chubbyphp-framework "^3.1" chubbyphp/chubbyphp-framework-router-fastroute
  2. Replace Chubbyphp\Framework\Router\FastRouteRouter with Chubbyphp\Framework\Router\FastRoute\Router.

SunriseRouter

  1. Upgrade: composer require chubbyphp/chubbyphp-framework "^3.1" chubbyphp/chubbyphp-framework-router-sunrise
  2. Replace Chubbyphp\Framework\Router\SunriseRouter with Chubbyphp\Framework\Router\Sunrise\Router.

Symfony Routing

  1. Upgrade: composer require chubbyphp/chubbyphp-framework "^3.1" chubbyphp/chubbyphp-framework-router-symfony
  2. Replace Chubbyphp\Framework\Router\SymfonyRouter with Chubbyphp\Framework\Router\Symfony\Router.

From 1.x to 2.x

Replace the code from the first block with the code of the second ones.

use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\ExceptionHandler;
use Chubbyphp\Framework\Middleware\MiddlewareDispatcher;
use Chubbyphp\Framework\Router\FastRouteRouter;

$app = new Application(
    new FastRouteRouter([$route]),
    new MiddlewareDispatcher(),
    new ExceptionHandler($responseFactory, true)
);
use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\Middleware\ExceptionMiddleware;
use Chubbyphp\Framework\Middleware\RouterMiddleware;
use Chubbyphp\Framework\Router\FastRouteRouter;

$app = new Application([
    new ExceptionMiddleware($responseFactory, true),
    new RouterMiddleware(new FastRouteRouter([$route]), $responseFactory),
]);

Copyright

Dominik Zogg 2020