Revision: Sat, 04 Feb 2023 12:24:33 GMT

HTTP - Installation and Configuration

The web application bundle (spiral/app) is shipped with a pre-configured HTTP component. You will need several extensions to enable it in alternative builds.

Installation

To install the extension:

composer require spiral/nyholm-bridge

Activate the extension by adding two bootloaders:

php
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.

Configuration

The HTTP extension can be configured via app/config/http.php file:

php
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:

php
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);
    }
}

Middleware

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).
Edit this page