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.
- 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
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-serverSee installation for the requirements and dependencies.
<?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.
- 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
composer install
composer check # phpcs, phpstan, phpunitThe MIT License (MIT). Please see License File for more information.