The web application bundle (spiral/app
) ships with a pre-configured HTTP component. You will need several extensions
to enable it in alternative builds.
To install the extension:
composer require spiral/nyholm-bridge
Activate the extension by adding two bootloaders:
class App extends Kernel
{
/*
* List of components and extensions to be automatically registered
* within system container on application start.
*/
protected const LOAD = [
// ...
// Fast PSR-7 implementation
\Spiral\Nyholm\Bootloader\NyholmBootloader::class,
// HTTP core
Spiral\Bootloader\Http\HttpBootloader::class,
// PSR-15 handler
Spiral\Bootloader\Http\RouterBootloader::class,
// ...
];
}
Note
See how to use custom PSR-15 handler here.
Make sure to configure routing.
The HTTP extension can be configured via app/config/http.php
file:
return [
// default base path
'basePath' => '/',
// default headers
'headers' => [
'Content-Type' => 'text/html; charset=UTF-8'
],
// application level middleware
'middleware' => [
// middleware class name
],
];
Note
The default configuration will be used if such file does not exist.
You can register Middleware during the bootload phase via HttpBootloader
:
namespace App\Bootloader;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Bootloader\Http\HttpBootloader;
use Spiral\Http\Middleware\JsonPayloadMiddleware;
class AppBootloader extends Bootloader
{
public function boot(HttpBootloader $http): void
{
// parse json payloads
$http->addMiddleware(JsonPayloadMiddleware::class);
}
}
HTTP extension includes multiple middlewares you might want to activate in your project:
Bootloader | Middleware |
---|---|
Spiral\Bootloader\Http\ErrorHandlerBootloader | Hide exceptions in non debug mode and render HTTP error pages. |
Spiral\Bootloader\Http\JsonPayloadsBootloader | Parse body of application/json requests. |
Spiral\Bootloader\Http\PaginationBootloader | Use request query parameters to automatically configure paginator(s). |
Spiral\Bootloader\Http\DiactorosBootloader | Use Zend/Diactoros as PSR-7 implementation (legacy). |