Why is Symfony2 (ezpublish5) not recognizing my controller as a service?

631 Views Asked by At

I'm trying to create a menu for my page with ezpublish5. I followed this tutorial http://partialcontent.com/Code/working-with-ez-publish-5-subrequests. I'm pretty new to Symfony, but I have my own bundle there and running, at least it is using my own pagelayout.html.twig.

I understand, what routing does, but I as far as I'm concerned in order to create a menu I need something else, probably a service, so I can do this in my twig-template

{{ render( controller( "myMenuController:myFunction" ) ) }}

So I add this to my my\Bundle\Resources\config\services.yml

parameters:
    my_root.menucontroller.class: my\Bundle\Controller\MenuController

services:
    my_root.controller:
        class: %my_root.menucontroller.class%
        arguments: [@ezpublish.view_manager]
        calls:
            - [setContainer, [@service_container] ]

    myalias:
        alias: my_root.controller

When I open it in the browser it says:

You have requested a non-existent service "myalias".") in "{% extends "Bundle::pagelayout.html.twig" %}

I checked other repos of ezp5 installations on github, they have pretty much the same yml-setup. I also realized, that when I make syntax errors on purpose in my services.yml it (while leaving out the call to the controller in the template) it doesn't change anything. Also I realized, that when I do the same in my\Bundle\DependencyInjection\myBundleExtension.php (which is supposed to load my services.yml file) it doesn't happen anything either.

So I'm getting the feeling something with bundle-setup is wrong, that somehow not everything is loaded correctly. But what could it be? Somewhere in the docu of symfony2 it says that the load-method in the Bundle\DependencyInjection\xyzExtension.php gets called automatically.

Would anyone have an idea of what could possibly be wrong with my setup? I'm really running out of ideas. And for the ezpublish5 part.. is this really the best way to create a menu right now?

1

There are 1 best solutions below

0
On

It does seem like your services are not being loaded.

Symfony relies on a naming convention to load xyzExtension.php. It's tripped me up a few times. You can stick a die statement in xyzExtension.load just to verify it is indeed being called.

If it is not being called than you can either change the extension class name to meet the convention or do what I do and just override the convention in your bundle class.

namespace Cerad\Bundle\GameV2Bundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

use Cerad\Bundle\GameV2Bundle\DependencyInjection\GameExtension;

class CeradGameV2Bundle extends Bundle
{   
    public function getContainerExtension()
    {
        return new GameExtension();
    }
}

And of course xyzExtension should have something like:

namespace Cerad\Bundle\GameV2Bundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Config\FileLocator;

class GameExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
    public function getAlias() { return 'cerad_game_v2'; }
}

From the command line you can verify your service is getting picked up:

app/console container:debug : grep my_root