Skip to content
This repository has been archived by the owner on May 2, 2022. It is now read-only.

glaubinix/symfony-template-response

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

symfony-template-response

Build status: Build Status

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.