glaubinix / symfony-template-response
symfony template response silex
Requires
- symfony/http-foundation: ^2.3|^3.0|^4.0
- symfony/templating: ^2.3|^3.0|^4.0
Requires (Dev)
- fabpot/php-cs-fixer: ^2.9
- phpunit/phpunit: ^6.5
- silex/silex: ^2.0
Suggests
- symfony/twig-bridge: Allows the usage of twig with the TwigEngine
README
Using the TemplateResponse will help you to build Controllers which are not tightly coupled to a certain Framework. In the case of Symfony/Silex this mostly means not injecting the ServiceContainer/Application to your Controllers.
Installation
The easiest way to install this library is to use Composer. Add the package "glaubinix/symfony-template-response" to your composer.json.
{ "require": { "glaubinix/symfony-template-response": "@stable" } }
Usage
To be able to use the TemplateResponse simply register the Listener.
Usage with symfony framework bundle
If you are using the framework bundle simply add this to your service config.
<service id="template_response.view_listener" class="Glaubinix\TemplateResponse\TemplateResponseListener%"> <argument type="service" id="templating" /> <tag name="kernel.event_listener" event="kernel.view" method="onKernelResponse" priority="10" /> </service>
Usage with silex and twig
If you are using silex make sure the templating Engine is registered then register the ServiceProvider which is provided with this library.
// In a Provider define the templating engine $app['templating'] = function(Container $app) { return new Symfony\Bridge\Twig\TwigEngine($app['twig'], new \Symfony\Component\Templating\TemplateNameParser()); }; // Register the provider $app->register(new Glaubinix\TemplateResponse\Silex\TemplateResponseServiceProvider()); // Use the response in $app->get('/', function() { return new TemplateResponse('template', []); });
Notes
To make this fully decoupled from Frameworks the TemplateResponse would not have to extend the Symfony Response and instead of simply rendering the content we would have to replace the Response. Once this is done a dedicated ResponseListener for every Framework would allow the usage in all Frameworks.
Maintaing sounds like a huge pain and I dont really see this happening :)
Original idea
First time I saw this idea was in QafooLabsNoFrameworkBundle the implementation is slightly different and depends on the symfony framework bundle which is main the reason I build a simple version for silex.