custom action in SonataAdminBundle

空扰寡人 提交于 2019-12-18 10:57:30

问题


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

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

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

My template for this link:

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

I dont' know how to solve this problems:

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

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

  2. Can I change generated url with generateUrl method?


回答1:


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

The controller,

//Vendor\YourBundle\Controller\EntityAdminController.php

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

In 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]


来源:https://stackoverflow.com/questions/10740303/custom-action-in-sonataadminbundle

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