简体   繁体   中英

Make a custom action in sonata admin bundle using CRUD controller

I want to make a custom page twig in Sonata admin bundle (clone for example ):

在此处输入图像描述

I use this tutorial:

http://symfony.com/doc/current/bundles/SonataAdminBundle/cookbook/recipe_custom_action.html

this is my controller CRUDController.php :

<?php
// src/AppBundle/Controller/CRUDController.php

namespace AppBundle\Controller;

use Sonata\AdminBundle\Controller\CRUDController as Controller;

class CRUDController extends Controller
{
    // ...
    /**
     * @param $id
     */
    public function cloneAction($id)
    {
        $object = $this->admin->getSubject();

        if (!$object) {
            throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
        }

        // Be careful, you may need to overload the __clone method of your object
        // to set its id to null !
        $clonedObject = clone $object;

        $clonedObject->setName($object->getName().' (Clone)');

        $this->admin->create($clonedObject);

        $this->addFlash('sonata_flash_success', 'Cloned successfully');

        return new RedirectResponse($this->admin->generateUrl('list'));

        // if you have a filtered list and want to keep your filters after the redirect
        // return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
    }
}

but when i click in clone i show this error:

在此处输入图像描述

can you help me..?

I feel like you forgot to configure your admin service for this page in the right way, please check http://symfony.com/doc/current/bundles/SonataAdminBundle/cookbook/recipe_custom_action.html#register-the-admin-as-a-service

cause sonata uses the SonataAdmin:CRUD controller by default and you should specify a custom one if you'd like to override the controller.

#src/AppBundle/Resources/config/admin.yml

services:
    app.admin.car:
        class: AppBundle\Admin\CarAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: Demo, label: Car }
        arguments:
            - null
            - AppBundle\Entity\Car
            - AppBundle:CRUD #put it here

You forget to configure Route for your controller. Sonata Admin has to know about your new Action in order to generate for it route. For this purposes you have to configure configureRoutes method in you admin class:

class CarAdmin extends AbstractAdmin  // For Symfony version > 3.1
{

    // ...

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

As you can see the name of the route matches the name of the action (but without action!) in your CRUDController. You had name of the action : 'cloneAction' so the name of the route is "clone".

In my case i forgot to add baseControllerName in app/config/admin.yml , before it was '~'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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