Integrating Silex with Mouf
In this example, we are using Silex and the Mouf framework as a container.
How?
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:
<?php 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:
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(); // Register Silex's controllers support $app->register(new Silex\Provider\ServiceControllerServiceProvider()); // Register the Mouf DI container $app->registerFallbackContainer(Mouf\MoufManager::getMoufManager()); // 'mycontroller' instance is declared in Mouf! $app->get('/hello', "mycontroller:testAction"); $app->run();
See how great it is? You can use the simple routing mechanism of Pimple and get rid of all the spaguetti code building your dependencies.
Found a typo? Something is wrong in this documentation? Just fork and edit it!