Custom action in SonataAdminBundle

On this page, I found a way to add a route for my custom action.

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

After that, I add a custom action to 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 do not know how to solve these problems:

  • How to define a user controller for this route? Now I have an error:

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

  • Is it possible to change the generated url using the generateUrl method?

+8
php symfony sonata-admin symfony-sonata
source share
1 answer

When you create a service for the EntityAdmin class, the third argument is the name of the controller. You can create a class that extends CRUDController and installs it in the service. eg

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] 
+30
source share

All Articles