PHP code example of derrabus / silex-psr11-provider

1. Go to this page and download the library: Download derrabus/silex-psr11-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/ */

    

derrabus / silex-psr11-provider example snippets



use Psr\Container\ContainerInterface;
use Rabus\Psr11ServiceProvider\Psr11ServiceProvider;
use Silex\Application;
use Silex\Provider\ServiceControllerServiceProvider;
use Symfony\Component\HttpFoundation\RedirectResponse;

class SomeController
{
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function someAction()
    {
        return new RedirectResponse(
            $this->container->get('url_generator')->generate('home')
        );
    }
}

$app = new Application();
$app->register(new ServiceControllerServiceProvider());
$app->register(new Psr11ServiceProvider());

$app['some_controller'] = function ($app) {
    // Inject a PSR-11 container instead of the $app
    return new SomeController($app['service_container']);
};

$app->get('/', function() {
    return 'Home.';
})->bind('home');

$app->get('/test1', 'some_controller:someAction');

// Type-hint the PSR-11 container instead of the $app
$app->get('/test2', function (ContainerInterface $container) {
    new RedirectResponse($container->get('url_generator')->generate('home'));
});