1. Go to this page and download the library: Download php-di/silex-bridge 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/ */
php-di / silex-bridge example snippets
= new DI\Bridge\Silex\Application();
$app->get('/hello/{name}', function ($name) use ($app) {
return 'Hello '.$app->escape($name);
});
$app->run();
class Mailer
{
// ...
}
$app->post('/register/{name}', function ($name, Mailer $mailer) {
$mailer->sendMail($name, 'Welcome!');
return 'You have received a new email';
});
// You can also inject the whole request object like in a traditional Silex application
$app->post('/register/{name}', function (Request $request, Mailer $mailer) {
// ...
});
// Injection works for middleware too
$app->before(function (Request $request, Mailer $mailer) {
// ...
});
// And param converters
$app->get('/users/{user}', function (User $user) {
return new JsonResponse($user);
})->convert('user', function ($user, UserManager $userManager) {
return $userManager->findById($user);
});
class HelloController
{
public function helloAction($name)
{
// ...
}
}
$app->get('/{name}', [HelloController::class, 'helloAction']);
class HelloController
{
public function __invoke($name)
{
// ...
}
}
$app->get('/{name}', HelloController::class);
class AuthMiddleware
{
public function beforeRoute(Request $request, Application $app)
{
// ...
}
}
$app->before([AuthMiddleware::class, 'beforeRoute']);
$containerBuilder = new DI\ContainerBuilder();
// E.g. setup a cache
$containerBuilder->setDefinitionCache(new ApcCache());
// Add definitions
$containerBuilder->addDefinitions([
// place your definitions here
]);
// Register a definition file
$containerBuilder->addDefinitions('config.php');
$app = new DI\Bridge\Silex\Application($containerBuilder);
$app->register(new Silex\Provider\TwigServiceProvider(), [
'twig.path' => __DIR__ . '/views',
]);
$app->get('/', function () use ($app) {
return $app['twig']->render('home.twig');
});
$builder = new ContainerBuilder();
$builder->addDefinitions([
'Twig_Environment' => \DI\get('twig'), // alias
]);
// ...
// Twig can now be injected in closures:
$app->post('/', function (Twig_Environment $twig) {
return $twig->render('home.twig');
});
$ composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.