覆盖symfony2捆绑控制器操作

时间:2013-06-01 15:01:52

标签: symfony

/src/Vendor/JobQueueBundle/Controller/DefaultController.php

namespace Vendor\JobQueueBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use JMS\JobQueueBundle\Controller\JobController;

/**
* @Route("/jobs")
*/
class DefaultController extends JobController
{
    /**
    * @Route("/index")
    */
    public function indexAction()
    {
        die();
    }
}

/app/config/routing.yml

vendor_api_job_queue:
    resource: "@VendorJobQueueBundle/Controller/"
    type:     annotation
    prefix:   /

JMSJobQueueBundle:
    resource: "@JMSJobQueueBundle/Controller/"
    type: annotation

/src/Syntetik/API/JobQueueBundle/SyntetikAPIJobQueueBundle.php

namespace Vendor\JobQueueBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class VendorJobQueueBundle extends Bundle
{
    public function getParent()
    {
        return 'JMSJobQueueBundle';
    }
}

当我尝试调用/ jobs / index时,我得到:

Method "JMS\JobQueueBundle\Controller\JobController::indexAction" does not exist.

DefaultController完全被忽略,不确定为什么?

谢谢!

2 个答案:

答案 0 :(得分:3)

已知并打开了JMSDiExtraBundle https://github.com/schmittjoh/JMSDiExtraBundle/issues/39的问题所以问题是,如果子类没有至少一个JMS注释,则DiExtarBundle不会查找注释的父类,因此不会在元数据缓存中生成代理类(查看app / cache / dev / jms_diextra / metadata /)

最快的解决方案是至少留下注释:

家长控制器>>

<?php

namespace Namespace\SiteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use JMS\DiExtraBundle\Annotation as DI;

class IndexController extends Controller 
{   
    /**
     * @DI\Inject("doctrine.orm.entity_manager")
     * @var \Doctrine\ORM\EntityManager $em
     */
    protected $em;

    /**
     * @DI\Inject("namespace.search.manager")
     * @var Namespace\SearchBundle\Services\SearchManager $searchManager
     */
    protected $searchManager;

    /**
     * @DI\Inject("namespace.product.manager")
     * @var Namespace\ProductBundle\Services\ProductManager $productManager
     */
    protected $productManager;

    /**
     * @Route("/", name="homepage")
     * @Template()
     */
    public function indexAction() { 
        echo "parent!";
        $defaultCategory = $this->searchManager->getDefaultCategory();
        ....
        return $result;
    }
}

儿童控制器&gt;&gt;

<?php

namespace OtherSpace\SiteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use JMS\DiExtraBundle\Annotation as DI;

use Namespace\SiteBundle\Controller\IndexController as BaseIndexController;


class IndexController extends BaseIndexController
{
/**
 * The temprary solution base on known JMS/DiExtraBundle open issue https://github.com/schmittjoh/JMSDiExtraBundle/issues/39
 * **We need to leave at leat one Inject in child class to get a proxy generated**  
 * 
 * @DI\Inject("doctrine.orm.entity_manager")
 * @var \Doctrine\ORM\EntityManager $em
 */
protected $em;

    /**
     * @Route("/", name="homepage")
     * @Template()
     */
    public function indexAction() { 
        echo "child!";
        $result = parent::indexAction();

        return $result;
    }

}

因此,这种方式将为SpaceOther-IndexController生成代理类,并且注释将起作用


我还解决了这个问题,您可以查看我的拉取请求https://github.com/schmittjoh/JMSDiExtraBundle/pull/153

答案 1 :(得分:1)

弄清楚这个问题。它似乎不是symfony2特有的。 问题是JMS \ DiExtraBundle \ JMSDiExtraBundle会搞乱周围的事情。只需删除该捆绑包,一切都可以通过本书。

谢谢!