I'm starting to use symfony 2 but I'd like to use moustache as the templating language instead of Twig or PHP. I wan't to use moustache because it's totally logicless and because I can also use it in javascript if I decide to handle the rendering of the template clientside.
How to do that?
Some extra info extending #m2mdas answer.
If you are not yet familiar with Symfony templating systems and bundle configuration take a look at these before you start coding:
How to expose a Semantic Configuration for a Bundle
Creating and using Templates
How to use PHP instead of Twig for Templates
And now a quick recipe to get you started. Take the following as loose examples, no need to stick with the names choosed.
1. Create a Resources/config/mustache.xml to define your services and to identify your template engine service (tag it as "templating.engine").
You could use Yaml and PHP instead of XML but the latter is prefered for "public" bundles.
<service id="mustache" class="Mustache">
<file>Mustache.php</file>
</service>
<service id="templating.engine.mustache" class="MustacheBundle\MustacheEngine" public="false">
<argument type="service" id="mustache" />
<argument type="service" id="templating.name_parser"/>
<argument type="service" id="templating.loader" />
<tag name="templating.engine" />
</service>
Examples:
Twig
PHP
Smarty
2. Create an Extension class to handle the semantic configuration for your bundle.
<?php
namespace MustacheBundle;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
class MustacheExtension extends Extension
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('mustache.xml');
// you may parse the $configs array here
// see: http://symfony.com/doc/current/cookbook/bundles/extension.html#parsing-the-configs-array
}
The presence of the previous class means that you can now define a mustache configuration namespace in any configuration file.
Examples:
Twig
Smarty
3. [Optional] Create a Configuration class to validate and merge configuration
<?php
namespace Mustache\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('mustache');
// see: http://symfony.com/doc/current/cookbook/bundles/extension.html#validation-and-merging-with-a-configuration-class
}
}
Examples:
Twig
Smarty
4. Create a MustacheEngine that implements EngineInterface
<?php
namespace MustacheBundle;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Templating\TemplateNameParserInterface;
use Symfony\Component\Templating\Loader\LoaderInterface;
use Symfony\Component\HttpFoundation\Response;
class MustacheBundle implements EngineInterface
{
public function __construct(\Mustache $mustache, TemplateNameParserInterface $parser, LoaderInterface $loader)
{
$this->mustache = $mustache;
$this->parser = $parser;
}
public function render($name, array $parameters = array())
{
$template = $this->load($name);
return $this->mustache->render($template);
}
// Renders a view and returns a Response.
public function renderResponse($view, array $parameters = array(), Response $response = null)
{
if (null === $response) {
$response = new Response();
}
$response->setContent($this->render($view, $parameters));
return $response;
}
// Returns true if the template exists.
public function exists($name)
{
try {
$this->load($name);
} catch (\InvalidArgumentException $e) {
return false;
}
return true;
}
// Returns true if this class is able to render the given template.
public function supports($name)
{
$template = $this->parser->parse($name);
return 'mustache' === $template->get('engine');
}
// Loads the given template.
// Should return the template name or a Mustache template object
protected function load($name)
{
$template = $this->parser->parse($name);
$template = $this->loader->load($template);
return (string) $template;
}
Examples:
Twig
PHP
Smarty
5. Enable your shiny new template engine in the application configuration file:
# app/config/config.yml
templating: { engines: ['twig', 'mustache'] }
6. Try it
<?php
// src/Acme/HelloBundle/Controller/HelloController.php
public function indexAction($name)
{
return $this->render('AcmeHelloBundle:Hello:index.html.mustache', array('name' => $name));
}
You may share a link to your bundle repository so we can track progress and help if needed. Good luck.
You have to create a class that implements EngineInterface and create create a DIC service named templating.engine.mustache to reference the class. And then in app/config.yml you can set default engine.
#app/config.yml
framework:
#.....
templating:
engines: ['mustache'] //mustache is the last portion of the service id
For reference you can check PhpEngine class and its service definition.
Related
I am struggling to get a specific service via class name from group of injected tagged services.
Here is an example:
I tag all the services that implement DriverInterface as app.driver and bind it to the $drivers variable.
In some other service I need to get all those drivers that are tagged app.driver and instantiate and use only few of them. But what drivers will be needed is dynamic.
services.yml
_defaults:
autowire: true
autoconfigure: true
public: false
bind:
$drivers: [!tagged app.driver]
_instanceof:
DriverInterface:
tags: ['app.driver']
Some other service:
/**
* #var iterable
*/
private $drivers;
/**
* #param iterable $drivers
*/
public function __construct(iterable $drivers)
{
$this->drivers = $drivers;
}
public function getDriverByClassName(string $className): DriverInterface
{
????????
}
So services that implements DriverInterface are injected to $this->drivers param as iterable result. I can only foreach through them, but then all services will be instantiated.
Is there some other way to inject those services to get a specific service via class name from them without instantiating others?
I know there is a possibility to make those drivers public and use container instead, but I would like to avoid injecting container into services if it's possible to do it some other way.
You no longer (since Symfony 4) need to create a compiler pass to configure a service locator.
It's possible to do everything through configuration and let Symfony perform the "magic".
You can make do with the following additions to your configuration:
services:
_instanceof:
DriverInterface:
tags: ['app.driver']
lazy: true
DriverConsumer:
arguments:
- !tagged_locator
tag: 'app.driver'
The service that needs to access these instead of receiving an iterable, receives the ServiceLocatorInterface:
class DriverConsumer
{
private $drivers;
public function __construct(ServiceLocatorInterface $locator)
{
$this->locator = $locator;
}
public function foo() {
$driver = $this->locator->get(Driver::class);
// where Driver is a concrete implementation of DriverInterface
}
}
And that's it. You do not need anything else, it just workstm.
Complete example
A full example with all the classes involved.
We have:
FooInterface:
interface FooInterface
{
public function whoAmI(): string;
}
AbstractFoo
To ease implementation, an abstract class which we'll extend in our concrete services:
abstract class AbstractFoo implements FooInterface
{
public function whoAmI(): string {
return get_class($this);
}
}
Services implementations
A couple of services that implement FooInterface
class FooOneService extends AbstractFoo { }
class FooTwoService extends AbstractFoo { }
Services' consumer
And another service that requires a service locator to use these two we just defined:
class Bar
{
/**
* #var \Symfony\Component\DependencyInjection\ServiceLocator
*/
private $service_locator;
public function __construct(ServiceLocator $service_locator) {
$this->service_locator = $service_locator;
}
public function handle(): string {
/** #var \App\Test\FooInterface $service */
$service = $this->service_locator->get(FooOneService::class);
return $service->whoAmI();
}
}
Configuration
The only configuration needed would be this:
services:
_instanceof:
App\Test\FooInterface:
tags: ['test_foo_tag']
lazy: true
App\Test\Bar:
arguments:
- !tagged_locator
tag: 'test_foo_tag'
Alternative to FQCN for service names
If instead of using the class name you want to define your own service names, you can use a static method to define the service name. The configuration would change to:
App\Test\Bar:
arguments:
- !tagged_locator
tag: 'test_foo_tag'
default_index_method: 'fooIndex'
where fooIndex is a public static method defined on each of the services that returns a string. Caution: if you use this method, you won't be able to get the services by their class names.
A ServiceLocator will allow accessing a service by name without instantiating the rest of them. It does take a compiler pass but it's not too hard to setup.
use Symfony\Component\DependencyInjection\ServiceLocator;
class DriverLocator extends ServiceLocator
{
// Leave empty
}
# Some Service
public function __construct(DriverLocator $driverLocator)
{
$this->driverLocator = $driverLocator;
}
public function getDriverByClassName(string $className): DriverInterface
{
return $this->driverLocator->get($fullyQualifiedClassName);
}
Now comes the magic:
# src/Kernel.php
# Make your kernel a compiler pass
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class Kernel extends BaseKernel implements CompilerPassInterface {
...
# Dynamically add all drivers to the locator using a compiler pass
public function process(ContainerBuilder $container)
{
$driverIds = [];
foreach ($container->findTaggedServiceIds('app.driver') as $id => $tags) {
$driverIds[$id] = new Reference($id);
}
$driverLocator = $container->getDefinition(DriverLocator::class);
$driverLocator->setArguments([$driverIds]);
}
And presto. It should work assuming you fix any syntax errors or typos I may have introduced.
And for extra credit, you can auto register your driver classes and get rid of that instanceof entry in your services file.
# Kernel.php
protected function build(ContainerBuilder $container)
{
$container->registerForAutoconfiguration(DriverInterface::class)
->addTag('app.driver');
}
For a few hours now I've been struggling to do the most simple thing you can imagine and it just won't work. I've read tons of stackoverflow questions, read the complete Symfony documentation on configuration files and with every article or other piece of information I read, it gets harder and harder to understand.
Details
I've created my own Bundle. Lets call it HappyBundle. I've put this Bundle in my company's folder. So naturally I've got CompanyHappyBundle.
I want to make a configuration file specifically for this bundle as I want it to be reusable.
As I test i created the following:
# src/Company/HappyBundle/Resources/config/config.yml
company_happy:
import:
path: /tmp
Now, what I want is to be able to use this value in my Controller. I just don't know how. It throws me the following error:
[Symfony\Component\Config\Exception\FileLoaderLoadException]
There is no extension able to load the configuration for "company_happy" (in /home/user/symfony/src/Company/HappyBundle/Resources/config/config.yml).
Looked for namespace "company_happy", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "debug", "web_profiler", "sensio_distribution" in /home/user/symfony/src/Company/HappyBundle/Resources/config/config.yml (which is being imported from "/home/user/symfony/app/config/config.yml").
Update
In the config.yml I added the following:
#app/config/config.yml
imports:
- { resource: "#CompanyHappyBundle/Resources/config/config.yml" }
I've also made a Configuration class because I read somewhere this was required. I really do think this is alot of work to make just one config file.
namespace Company\HappyBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* {#inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('company_happy');
$rootNode
->children()
->arrayNode('import')
->children()
->scalarNode('attachments_path')->defaultValue('/tmp')->end()
->scalarNode('method')->defaultValue('ALL')->end()
->booleanNode('move_mail')->defaultValue(true)->end()
->booleanNode('mark_read')->defaultValue(true)->end()
->end()
->end()
;
return $treeBuilder;
}
}
What I am actually looking for are the steps and requirements needed to get this working. The thing with symfony is that it has a million ways to do this. The documentation doesn't just give a workflow.
Can someone please help me out?
I have solved my own issue but not without trouble. I'm not at all pleased with Symfony's configuration system.
Step one - Create your config file
Create a file named config.yml in src/<bundle name>/Resources/config/
yourbundle:
param_one: value_one
param_two: value_two
param_three: value_three
param_four: value_four
param_five:
subparam_one: subvalue_one
subparam_two: subvalue_two
subparam_three: subvalue_three
subparam_four: subvalue_four
Step two - Importing your configuration file
Go to app/config/config.yml and add:
#app/config/config.yml
imports:
- { resource: "#YourBundle/Resources/config/config.yml" }
Step three - Create a configuration class
Create a file named Configuration.php in src/<bundle name>/DependencyInjection/
namespace YourBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* {#inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('yourbundle');
$rootNode
->children()
->scalarNode('param_one')->defaultValue('value_one')->end()
->scalarNode('param_two')->defaultValue('value_two')->end()
->scalarNode('param_three')->defaultValue('value_three')->end()
->scalarNode('param_four')->defaultValue('value_four')->end()
->arrayNode('param_five')
->children()
->scalarNode('subparam_one')->defaultValue('subvalue_one')->end()
->scalarNode('subparam_two')->defaultValue('subvalue_two')->end()
->scalarNode('subparam_three')->defaultValue('subvalue_three')->end()
->scalarNode('subparam_four')->defaultValue('subvalue_four')->end()
->end()
->end()
;
return $treeBuilder;
}
}
Step four - Creating an extension
Last but not least, you'll have to create an extension. Create a file <yourbundle>Extension.php in src/<your bundle>/DependencyInjection/
namespace YourBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
class YourbundleExtension extends Extension
{
/**
* #var ContainerBuilder
*/
protected $container;
/**
* {#inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$this->container = $container;
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
foreach ($config as $key => $value) {
$this->parseNode('yourbundle.'.$key, $value);
}
$container->setParameter('yourbundle', $config);
}
/**
* #param string $name
* #param mixed $value
*
* #throws \Exception
*/
protected function parseNode($name, $value)
{
if (is_string($value)) {
$this->set($name, $value);
return;
}
if (is_integer($value)) {
$this->set($name, $value);
return;
}
if (is_array($value)) {
foreach ($value as $newKey => $newValue) {
$this->parseNode($name.'.'.$newKey, $newValue);
}
return;
}
if (is_bool($value)) {
$this->set($name, $value);
return;
}
throw new \Exception(gettype($value).' not supported');
}
/**
* #param string $key
* #param mixed $value
*/
protected function set($key, $value)
{
$this->container->setParameter($key, $value);
}
}
All these steps are required just to be able to call a configuration parameter specific for your bundle.
If any of you know any way to do this easier, feel free to post an answer or comment.
A few notices:
In config.yml, you are trying to define import as array. It seems like symfony doesn't allow creating array elements in the root of your config, meaning that you have to nest arrays deeper down the tree. So you can not do:
company_happy:
import:
path: /tmp
another_import:
...
I am not sure this is exactly what you were trying to do, but you defined import as array, which makes me assume so.
On the other hand, you can do:
company_happy:
imports:
import:
path: /tmp
another_import:
...
Regarding no extension able to load the configuration error: Make sure your extension file is following naming convetions.It should be called CompanyHappyExtension.php with CompanyHappyExtension class defined inside.
I have created a sample CompanyHappyBundle bundle which is working fine on Symofny 3 (probably works on S2 as well). Feel free to clone/download it :)
The services.yml file is an added bonus, as you will most likely need it anyway.
src/Company/Bundle/HappyBundle/CompanyHappyBundle.php:
<?php
namespace Company\Bundle\HappyBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class CompanyHappyBundle extends Bundle
{
}
src/Company/Bundle/HappyBundle/DependencyInjection/CompanyHappyExtension.php
<?php
namespace Company\Bundle\HappyBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
class CompanyHappyExtension extends Extension implements ExtensionInterface
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$configuration = new Configuration();
$options = $this->processConfiguration($configuration, $configs);
// Do something with your options here
}
}
src/Company/Bundle/HappyBundle/DependencyInjection/Configuration.php
<?
namespace Company\Bundle\HappyBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('company_happy');
$rootNode
->children()
->arrayNode('imports')
->prototype('array')
->children()
->scalarNode('path')->defaultValue('/tmp')->end()
->scalarNode('method')->defaultValue('ALL')->end()
->booleanNode('move_mail')->defaultValue(true)->end()
->booleanNode('mark_read')->defaultValue(true)->end()
->end()
->end()
->end()
;
return $treeBuilder;
}
}
src/Company/Bundle/HappyBundle/Resources/config/config.yml
company_happy:
imports:
import:
path: /tmp
src/Company/Bundle/HappyBundle/Resources/config/services.yml
# Define your services here
services:
you're almost done, you just need to configure your Bundle to use your config parameters, take a look at this answer.
I will explain with an example:
There are 2 bundles : Foo\SecurityBundle and Foo\MenuBundle.
Foo\MenuBundle has Menu class that looks like this:
namespace Foo\MenuBundle;
use Foo\SecurityBundle\MenuSecurer; //note this
class Menu{
protected $securer;
public function __construct(MenuSecurer $securer = null){
$this->securer = $securer;
}
public function buildMenu(){
//build the $menu ...
//...
if($this->securer != null)
$securer->secure($menu);
}
}
the security bundle will automatically inject the $menuSecurer if it is installed,
however the problem is When the security bundle is not installed then it's classes aren't defined either, so i can't use Foo\SecurityBundle... in the MenuBundle even though I don't really use it.what's the correct way around this?
There is a section in the symfony docs dealing with this this situation:
http://symfony.com/doc/current/book/service_container.html#optional-dependencies-setter-injection
If you have optional dependencies for a class, then "setter injection"
may be a better option.
According to this your class might look like this:
namespace Foo\MenuBundle;
class Menu{
protected $securer;
public function setSecurer($securer) {
$this->securer = $securer;
}
public function buildMenu(){
//build the $menu ...
//...
if($this->securer != null)
$securer->secure($menu);
}
}
# config.yml
menu_service:
class: Foo\MenuBundle\Menu
calls:
- [setMailer, ["#securer"]]
Something like this...
Unfortunately you still cannot have a use statement without an interface you know is existing.
There are quite a few ways to go about this, but I think a good approach would be to add a configuration setting for the class/service to be injected into the constructors first argument.
For example, in the Foo\MenuBundle\Menu class (assuming this is defined as a service already), you could add an additional item to the bundle's configuration to define a default service for the $securer, then optionally override it in the config if desired.
In the configuration class (DependecyInjection\Configuration.php):
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('foo_menu');
$rootNode
->children()
->arrayNode('service')
->addDefaultsIfNotSet()
->children()
->scalarNode('menu_securer')->defaultValue('foo_security.menu_securer')->end()
>end()
->end()
->end();
return $treeBuilder;
}
In the extension class (DependecyInjection\FooMenuExtension.php):
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
// ...
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
// This is what sets 'foo_menu.menu_securer' to the service you want
foreach ($config['service'] as $key => $service) {
$container->setAlias($this->getAlias() . '.' . $key, $service);
}
$loader->load('services.xml');
$container->getDefinition('foo_menu.menu.menu')
->replaceArgument(0, new Reference('foo_menu.menu_securer'));
}
And your service definition would just look something like this...
<service id="foo_menu.menu.menu" class="%foo_menu.menu.menu.class%">
<argument /> <!-- foo_menu.menu_securer -->
</service>
Now in your config.yml you can just switch out the service you want to use by defining it in under...
foo_menu:
service:
menu_securer: 'some_other.service'
Edit: Regarding the type hinting, as mentioned by Markus, it would probably be a good idea to implement an interface that the $securer must implement.
I have a Symfony2 project and I am using Translation component for translating text. I have all translations in yml file like so
translation-identifier: Translated text here
Translating text looks like this from Twig
'translation-identifier'|trans({}, 'domain')
The thing is, in some cases I would like to have two different texts for same translation (not for pluralization). Here's how I would like it to work:
Define two texts in yml file for translations that need to have different texts. Each would have it's own unique suffix
translation-identifier-suffix1
translation-identifier-suffix2
Define a global rule that would define which suffix should be choosen. Psuedocode below:
public function getSuffix() {
return rand(0, 10) < 5 ? '-suffix1' : '-suffix2';
}
Twig (and PHP) would look the same - I would still specify just the identifier without suffix. Translator would then append suffix to the identifier and try to find a match. If there would be no match it would try to find a match again without suffix.
AFAIK, Translator component doesn't support it.
But if you want same kind of behavior, you could do by overriding the translator service.
1) Override the service
# app/config/config.yml
parameters:
translator.class: Acme\HelloBundle\Translation\Translator
First, you can set the parameter holding the service's class name to your own class by setting it in app/config/config.yml.
FYI: https://github.com/symfony/FrameworkBundle/blob/master/Resources/config/translation.xml
2) Extend the translator class provided symfony framework bundle.
FYI: https://github.com/symfony/FrameworkBundle/blob/master/Translation/Translator.php
3) Overwrite the trans function which is provider by translator component.
https://github.com/symfony/Translation/blob/master/Translator.php
Hope this helps!
Here is the extended translator class in case anyone ever needs it
<?php
namespace Acme\HelloBundle\Translation;
use Symfony\Bundle\FrameworkBundle\Translation\Translator as BaseTranslator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Translator extends BaseTranslator {
const SUFFIX_1 = '_suffix1';
const SUFFIX_2 = '_suffix2';
private $suffix;
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array()) {
parent::__construct($container, $selector, $loaderIds, $options);
$this->suffix = $this->getSuffix($container);
}
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null) {
if ($locale === null)
$locale = $this->getLocale();
if (!isset($this->catalogues[$locale]))
$this->loadCatalogue($locale);
if($this->suffix !== null && $this->catalogues[$locale]->has((string) ($id . $this->suffix), $domain))
$id .= $this->suffix;
return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
}
private function getSuffix($container) {
return rand(0, 10) < 5 ? self::SUFFIX_1 : self::SUFFIX_2;
}
}
?>
As of Symfony 3, Venu's answer no longer works completely, as the translator.class parameter is no longer used.
To load your custom translator class, you now need to create a compiler pass.
<?php
namespace Acme\HelloBundle\DependencyInjection\Compiler;
use Acme\HelloBundle\Translation\Translator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class TranslatorOverridePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container->getDefinition('translator.default')->setClass(Translator::class);
}
}
And this compiler pass needs to be added to the container.
<?php
namespace Acme\HelloBundle;
use Acme\HelloBundle\DependencyInjection\Compiler\TranslatorOverridePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeHelloBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new TranslatorOverridePass());
}
}
So, here is a controller I've just built:
namespace MDP\API\ImageBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class RetrieverController {
private $jsonResponse;
private $request;
public function __construct(JsonResponse $jsonResponse, Request $request) {
$this->jsonResponse = $jsonResponse;
$this->request = $request;
}
/**
* #Route("/image/{amount}")
* #Template("MDPAPIImageBundle:Retriever:index.json.twig")
*/
public function retrieve($amount)
{
}
}
I want to make this controller work as a service, to use DependencyInjection. So, here is my services.xml file:
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="mdpapi_image.json_response" class="Symfony\Component\HttpFoundation\JsonResponse" />
<service id="mdpapi_image.request" class="Symfony\Component\HttpFoundation\Request" />
<service id="mdpapi_image.controller.retriever" class="MDP\API\ImageBundle\Controller\RetrieverController">
<argument type="service" id="mdpapi_image.json_response" />
<argument type="service" id="mdpapi_image.request" />
</service>
</services>
</container>
However, when I try to execute my controller, I always get this exception:
Catchable Fatal Error: Argument 1 passed to MDP\API\ImageBundle\Controller\RetrieverController::__construct() must be an instance of Symfony\Component\HttpFoundation\JsonResponse, none given, called in /home/steve/projects/APIs/app/cache/dev/jms_diextra/controller_injectors/MDPAPIImageBundleControllerRetrieverController.php on line 13 and defined in /home/steve/projects/ImageAPI/ImageBundle/Controller/RetrieverController.php line 13
When I am in dev mode, I see that Symfony generate this file in the cached files...
class RetrieverController__JMSInjector
{
public static function inject($container) {
$instance = new \MDP\API\ImageBundle\Controller\RetrieverController();
return $instance;
}
}
How can I make it so that the arguments are added correctly to the controller, like specified in my services.xml file?
Just found the answer to your question, hope this helps you (or others who find this question)
<?php
namespace MDP\API\ImageBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
/**
* #Route("/image", service="mdpapi_image.controller.retriever")
*/
class RetrieverController {
private $jsonResponse;
private $request;
public function __construct(JsonResponse $jsonResponse, Request $request) {
$this->jsonResponse = $jsonResponse;
$this->request = $request;
}
/**
* #Route("/{amount}")
* #Template("MDPAPIImageBundle:Retriever:index.json.twig")
*/
public function retrieve($amount)
{
}
}
Sources:
http://richardmiller.co.uk/2011/10/25/symfony2-routing-to-controller-as-service-with-annotations/
http://symfony.com/doc/current/cookbook/controller/service.html
So, I fixed my problem. I had to stop using Annotations in my controller and changed my routing.yml so write the routes directly.
image_retrieve:
pattern: /image/{amount}
defaults: { _controller: mdp_api_image_retriever_retrieve:retrieve }
requirements:
_method: GET
That fixed the whole problem. The problem with annotations is that on this class (JMS\DiExtraBundle\HttpKernel\ControllerResolver) on line 90, in the Symfony core, you see this core:
// If the cache warmer tries to warm up a service controller that uses
// annotations, we need to bail out as this is handled by the service
// container directly.
if (null !== $metadata->getOutsideClassMetadata()->id
&& 0 !== strpos($metadata->getOutsideClassMetadata()->id, '_jms_di_extra.unnamed.service')) {
return;
}
Then on line 69, it tries to call the method call_user_func from the returned data, which was null.
In other words, using Annotation and creating your Controllers as Service does NOT work together. I lost 4 hours debugging this issue, so I hope this might help someone in the future :)
Seems like you forgot to write an extension class that loads your services.xml file:
namespace MDP\API\ImageBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\Config\FileLocator;
class ImageExtension extends Extension
{
/**
* #param array $configs
* #param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('services.xml');
}
}