简体   繁体   English

Sonata Admin Class:添加指向Admin类的KnpMenu链接和自定义路由

[英]Sonata Admin Class : add KnpMenu links pointing Admin class with custom route

Using SonataAdminBundle with Symfony2, I'm looking for a solution to access some Admin classes with a specific route. 使用SonataAdminBundle与Symfony2,我正在寻找一个解决方案来访问具有特定路由的一些Admin类。

For example, I have a ContractAdmin class with boolean fields such as "Enabled". 例如,我有一个ContractAdmin类,其中包含布尔字段,例如“Enabled”。 What I would like is to add in the left KnpMenu of sonata admin, some links pointing to the same Admin class but with a custom route (other than the default "list" route), for example: 我想要的是在sonata admin的左侧KnpMenu中添加一些链接,指向同一个Admin类但使用自定义路由(默认的“list”路由除外),例如:

  • Contracts 合同
    • All Contracts 所有合同
    • Contracts enabled (Listing only enabled contract) 已启用合约(仅列出启用的合约)
    • Contracts not yet enabled (Listing only not enabled contract) 合同尚未启用(仅列出未启用的合同)

This would avoid me to use filters. 这样可以避免我使用过滤器。

So, how could I create and put these links to the menu which target the corresponding admin class controller with a custom route? 那么,我怎样才能创建这些链接并将其放到菜单中,该菜单使用自定义路径定位相应的管理类控制器?

Thank you ;) 谢谢 ;)

I've solved it declaring a custom CRUDController for this admin class and adding the actions needed calling listAction method : 我已经解决了为这个管理类声明一个自定义CRUDController并添加调用listAction方法所需的操作:

class ContractAdminController extends Controller {

public function contractsEnabledAction() {
    return $this->listAction();
}

I've declared this custom route into the Admin class : 我已将此自定义路由声明为Admin类:

protected function configureRoutes(RouteCollection $collection) {
    parent::configureRoutes($collection);
    $collection->add('contracts_enabled', 'contractsEnabled/');
}

Then, overriding the createQuery method in the admin class, I'm using the request "_route" attribute like that : 然后,覆盖admin类中的createQuery方法,我正在使用请求“_route”属性:

public function createQuery($context = 'list') {
    $query = parent::createQuery($context);

    switch ($this->getRequest()->get("_route")) {
        case "admin_acme_contract_contracts_enabled" :
            $query->andWhere(
                    $query->expr()->eq($query->getRootAliases()[0] . '.enabled', ':param')
            );
            $query->setParameter('param', true);
            break;
    }
    return $query;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM