Integrating Silex with Mouf

In this example, we are using Silex and the Mouf framework as a container.


Here is a sample about injecting a controller in Pimple.

  • Install this package using Composer.
  • This package depends on Mouf. Once Mouf is downloaded, you need to install Mouf.
  • Then, declare a simple test controller:

    namespace Example\Controller;
    use Symfony\Component\HttpFoundation\JsonResponse;
    class TestController
      private $text;
      public function __construct($text)
          $this->text = $text;
      public function testAction()
          return new JsonResponse(array("hello"=>$this->text));
  • Create an instance mycontroller for your controller in Mouf. When this is over, you should see this in Mouf UI:
    Controller's instance
  • Init your application using the extended Mouf\Silex\Application class:

    // Load Mouf (and Composer's autoloader)
    require_once __DIR__.'/mouf/Mouf.php';
    // Get Silex app with Mouf support
    $app = new Mouf\Interop\Silex\Application(Mouf\MoufManager::getMoufManager());
    // Register Silex's controllers support
    $app->register(new Silex\Provider\ServiceControllerServiceProvider());
    // 'mycontroller' instance is declared in Mouf!
    $app->get('/hello', "mycontroller:testAction");

See how great it is? You can use the simple routing mechanism of Pimple and get rid of all the spaghetti code building your dependencies.

Found a typo? Something is wrong in this documentation? Just fork and edit it!