Extending Symfony2 Controller Resolver

半城伤御伤魂 提交于 2019-12-07 12:31:03

问题


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);
}


来源:https://stackoverflow.com/questions/25218003/extending-symfony2-controller-resolver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!