扩展Symfony2控制器解析器 [英] Extending Symfony2 Controller Resolver

查看:84
本文介绍了扩展Symfony2控制器解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在创建一个包,如果请求是Ajax请求,则该包可以将fooAction重命名为fooAjaxAction. 正如那个问题的答案所说,我必须扩展控制器解析器类. 如果更改config.yml中的controller_resolver.class,我将得到一个ResourceNotFoundException.但是如果没有,我不会有任何错误(但是没有覆盖,所以这不是我想要的)

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)

我的问题是:如何注册并使用新的控制器解析器?我是正确的 ?错误吗?

这就是我所做的:

您可以在packagist中找到要测试的捆绑软件,并通过以下方式下载:

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

在AppKernel.php中激活它:

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

配置

# 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.

推荐答案

getArguments方法缺少返回值(由于在执行其他回答时丢失了返回值),这意味着控制器解析器实际上并没有得到任何要解决的论点.

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

这篇关于扩展Symfony2控制器解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆