Skip to content

Instantly share code, notes, and snippets.

@derrabus
Created February 5, 2018 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derrabus/2e5f7fe7700e27498f417d56bac39fee to your computer and use it in GitHub Desktop.
Save derrabus/2e5f7fe7700e27498f417d56bac39fee to your computer and use it in GitHub Desktop.
DI-enabled Silex Controller
<?php
namespace Acme\MyApp;
use Silex\Application;
use Silex\Provider\ServiceControllerServiceProvider;
use Silex\Provider\TwigServiceProvider;
$app = new Application();
$app->register(new TwigServiceProvider());
$app->register(new ServiceControllerServiceProvider());
$app['my_controller'] = function($app) {
return new Controller\MyController($app['twig']);
};
$app->get('/my_route', 'my_controller:myAction');
<?php
namespace Acme\MyApp\Controller;
use Twig\Environment;
class MyController
{
private $twig;
public function __construct(Environment $twig)
{
$this->twig = $twig;
}
public function myAction(): string
{
// Insert controller logic here.
return $this->twig
->render('my_template.html.twig', $parameters);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment