solophp / application
Application bootstrap with PSR-15 middleware pipeline
Requires
- php: ^8.3
- psr/container: ^2.0
- psr/http-factory: ^1.1
- psr/http-message: ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- solophp/contracts: ^1.1
- solophp/router: ^4.0
Requires (Dev)
- nyholm/psr7: ^1.8.2
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.5 || ^12.0
- solophp/container: ^2.1 || ^3.0
- squizlabs/php_codesniffer: ^4.0
Suggests
- nyholm/psr7: PSR-7/PSR-17 implementation
- nyholm/psr7-server: ServerRequest creator from globals
- solophp/container: Default container implementation
- solophp/http-emitter: Default HTTP emitter implementation
Provides
None
Conflicts
None
Replaces
None
README
PSR-15 application kernel that wires your container, middleware and routes into one request handler, with routing by Solo Router 4.
Full documentation: solophp.github.io/Application. Upgrading from 3.x? Read the upgrade guide.
Features
- PSR-15 pipeline — Global middleware run outermost first and are resolved from the container only when a request reaches them
- Built-in routing —
RoutingMiddlewarematches the route, runs group and route middleware, and puts the handler, parameters, route name and attributes on the request - Routing errors as exceptions —
404,405withAllowand400are thrown asRoutingException; answer them withRoutingErrorMiddlewareor render them in your own error middleware - Route cache — The built router is kept in a PHP file that OPcache holds in memory, so a request skips registering and compiling routes
- Service providers — Plain classes with a
register()method fill any container that implementsWritableContainerInterface - Flexible handlers — Controller methods and invokable classes from the container, or any callable
- Workers and tests —
Applicationis a PSR-15 request handler:handle()returns the response,run()emits it
Installation
composer require "solophp/application:^4.0.1"
Requires PHP 8.3 or later, a PSR-7/PSR-17 implementation, a container and, for run(), an HTTP emitter. The quick start uses these:
composer require solophp/container solophp/http-emitter nyholm/psr7 nyholm/psr7-server
See installation for the requirements and dependencies.
Quick Example
<?php // public/index.php use App\Providers\HttpServiceProvider; use Solo\Application\Application; use Solo\Application\Config; use Solo\Application\RoutingErrorMiddleware; use Solo\Application\RoutingMiddleware; require __DIR__ . '/../vendor/autoload.php'; $app = new Application(new Config( basePath: dirname(__DIR__), routesPath: dirname(__DIR__) . '/app/routes.php', providers: [HttpServiceProvider::class], middleware: [RoutingErrorMiddleware::class, RoutingMiddleware::class], )); $app->run();
<?php // app/routes.php use App\Controller\UserController; use Solo\Router\RouteCollector; return function (RouteCollector $r): void { $r->get('/users/{id}', [UserController::class, 'show'])->name('users.show'); };
The controller comes from the container and gets the request, a new response and the route parameters:
<?php namespace App\Controller; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; final class UserController { public function show(ServerRequestInterface $request, ResponseInterface $response, array $params): ResponseInterface { $response->getBody()->write("User {$params['id']}"); return $response; } }
HttpServiceProvider registers the PSR-17 response factory, the incoming request and the emitter: see the quick start.
Documentation
- Quick start
- Lifecycle
- Configuration
- Upgrading from 3.x
- Routing
- Route cache
- Routing errors
- Middleware
- Service providers
- Route handlers
- Workers and emitting
- Custom container
- Testing
- API reference
Development
composer install
composer check # phpcs, phpstan, phpunit
License
The MIT License (MIT). Please see License File for more information.