PHP code example of bnf / zend-diactoros-service-provider

1. Go to this page and download the library: Download bnf/zend-diactoros-service-provider library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bnf / zend-diactoros-service-provider example snippets


new Container([
    new \Bnf\ZendDiactoros\ServiceProvider,
    new YouServiceProvider,
]);


declare(strict_types = 1);
endDiactoros\ServiceProvider as ZendDiactorosServiceProvider;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Interop\Container\ServiceProviderInterface;

class Service
{
    private $responseFactory;

    public function __construct(ResponseFactoryInterface $responseFactory)
    {
        $this->responseFactory = $responseFactory;
    }

    public function create404Response()
    {
        $this->responseFactory->createResponse(404);
    }
}

$container = new Container([
    new ZendDiactorosServiceProvider,

    // Register own services and configuration
    new class implements ServiceProviderInterface {
        public function getFactories(): array
        {
            return [
                Service::class => function (ContainerInterface $container): Service {
                    return new Service($container->get(ResponseFactoryInterface::class));
                }
            ];
        }
        public function getExtensions(): array
        {
            return [];
        }
    }
]);

$service = $container->get(Service::class);
var_dump($service->create404Response());