Extending Symfony2 Controller Resolver

2019-05-06 15:38发布

问题:

I am currently creating a bundle which can rename fooAction into fooAjaxAction if request is an Ajax Request. As the answer of that question says, I have to extend the controller resolver class. I have a ResourceNotFoundException if I change the controller_resolver.class in config.yml . but if I don't, I don't have any errors (but there is no override so this is not what I want)

My questions are : how can i register my new controller resolver and use it ? I am right ? Wrong ?

This is what I've done :

You can find my Bundle for testing in packagist and download it via :

composer require "/prefix-bundle":"dev-dev"

activate it in AppKernel.php:

<?php 
// AppKernel.php
new \PrefixBundle\PrefixBundle()

Config

# App/Config/config.yml
parameters:
    controller_resolver.class: PrefixBundle\Component\Controller\ControllerResolver

So this is my custom Controller.

<?php 
namespace \PrefixBundle\Component\Controller;

use Symfony\Component\HttpKernel\ControllerControllerResolver as BaseControllerResolver;
use Symfony\Component\HttpFoundation\Request;

class ControllerResolver extends BaseControllerResolver
{

    public function getArguments(Request $request, $controller)
    {
        parent::getArguments($request, $controller);
    }
}

I am assuming that this controller is doing nothing for instance, I will add logic in the future.

回答1:

The return is missing from the getArguments method (due to me missing it when I did the other answer) meaning that the controller resolver isn't actually getting any arguments to resolve.

public function getArguments(Request $request, $controller)
{
    // Should have the return..
    return parent::getArguments($request, $controller);
}