SonataAdminBundle中的自定义操作 [英] custom action in SonataAdminBundle

查看:74
本文介绍了SonataAdminBundle中的自定义操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个页面上,我发现了如何为我的自定义操作添加路线.

On this page I found how to add route for my custom action.

protected function configureRoutes(RouteCollection $collection) {
    $collection->add('ispremium', $this->getRouterIdParameter().'/ispremium'); 
}

此后,我在Admin类中添加了自定义操作:

After that I add custom action in my Admin class:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('id')
        ->add('code', null, array('label' => 'Code'))
        ->add('_action', 'actions', array( 
            'actions' => array(  
                'ispremium' => array(
                    'template' => 'AppMyBundleBundle:Admin:ispremium.html.twig'
                )
            )
        ))
    ;
}

它生成的网址是这样的:

It generated url like this:

/app_dev.php/admin/mobispot/discodes/discode/300876/ispremium

此链接的模板:

<a href="{{ admin.generateObjectUrl('ispremium', object) }}">Link</a>

我不知道如何解决这个问题:

I dont' know how to solve this problems:

  1. 如何为该路径传递定义自定义控制器? 现在我有一个错误:

  1. How to define custom controller for that route pass? Now I have an error:

方法"Sonata \ AdminBundle \ Controller \ CRUDController :: ispremiumAction"不存在.

Method "Sonata\AdminBundle\Controller\CRUDController::ispremiumAction" does not exist.

我可以使用generateUrl方法更改生成的URL吗?

Can I change generated url with generateUrl method?

推荐答案

EntityAdmin类创建服务时,第三个参数是控制器名称.您可以创建扩展CRUDController的类并将其设置为服务.例如

When you are creating service for EntityAdmin class the third argument is the controller name. You can create a class that extends CRUDController and set it in service. e.g

控制器

//Vendor\YourBundle\Controller\EntityAdminController.php

class EntityAdminController extends CRUDController
{
    public function ispremiumAction()
    {
        //process
    }
}

services.yml中,

entity.admin.service:
  class: FQCN\Of\EntityAdmin
  tags:
    - { name: sonata.admin, manager_type: orm, group: your_group, label: Label }
  arguments: [null, FQCN\Of\Entity, VendorYourBundle:EntityAdmin]

这篇关于SonataAdminBundle中的自定义操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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