2
votes

I tried to use controllers as a service in my Symfony 2.7 project.

This was my previous routing.yml before i tried the services:

affiliate_api_get_adblock:
    path:     /api/1.0/admin/affiliate/adblock
    defaults: { _controller: AffiliateBundle:AffiliateApi:adblock }

It works perfectly. It routes the path to the AffiliateApiController in the AffiliateBundle and executes the adblockAction-Method.

Then i created the following service:

services:
    company.affiliate_api_controller:
        class: CompanyName\AffiliateBundle\Controller\AffiliateApiController
        arguments:
            - @doctrine.orm.entity_manager

When I want to instantiate the controller per DI-Container, it works: $c = $this->get('company.affiliate_api_controller');

But when i then try to use the service name in the routing.yml instead of the class name as described in the Symfony docs, errors where thrown:

affiliate_api_get_adblock:
    path:     /api/1.0/admin/affiliate/adblock
    defaults: { _controller: company.affiliate_api_controller:adblock }

What happens then requesting this route, is a 500 error with the following message:

Controller "company.affiliate_api_controller:adblock" for URI "/api/1.0/admin/affiliate/adblock" is not callable. 500 Internal Server Error - InvalidArgumentException

What's wrong here?

1

1 Answers

3
votes

Try to declare the defaults in your routing.yml with the suffix Action:

affiliate_api_get_adblock:
    path:     /api/1.0/admin/affiliate/adblock
    defaults: { _controller: company.affiliate_api_controller:adblockAction }