fudge / silex-container-aware
Provides Symfony2 Container Aware controllers within Silex
Installs: 153
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 1
Forks: 1
Open Issues: 0
Type:service-provider
Requires
- php: >= 5.4
- silex/silex: ~1.1
Requires (Dev)
This package is not auto-updated.
Last update: 2025-01-14 07:52:15 UTC
README
Provides Symfony2 Container Aware controllers within Silex.
This may seem redudant as you are given the Application instance per method call, but this allows you to keep your code DRY as reusable methods can be implemented involving external services, such as redirects and rendering.
Rest assured that this package is covered 100% by Unit Tests, and does follow Semantic Versioning (unlike others!).
Examples below.
Requirements
- PHP 5.4+
- Silex ~1 (Obviously!)
Examples
This is a very naive example, showing the accessibility of the Container within a ContainerAware Controller
Silex - index.php
<?php require __DIR__.'/vendor/autoload.php'; use Silex\Application; use Silex\Provider\ServiceControllerServiceProvider; use Fudge\SilexComponents\ContainerAware\ContainerAwareServiceProvider; $app = new Application; $app->register(new ContainerAwareServiceProvider); $app->register(new ServiceControllerServiceProvider); $app->get("/", "IndexController::hello");
IndexController.php
<?php class IndexController extends \Fudge\SilexComponents\ContainerAware\Controller { public function hello() { return $this->render("foo.html.twig"); } protected function render($templateName) { return $this->get("twig")->render($templateName); } protected function get($service) { return $this->getContainer()[$service]; } }
Roadmap
- Implement more features to the ContainerAware Controller
- Potentially extend the functionality of the Silex\Application to allow automatic dependency injection, similar to Laravel