Php 使用CRUD控制器在sonata管理包中进行自定义操作

Php 使用CRUD控制器在sonata管理包中进行自定义操作,php,symfony,sonata-admin,symfony-sonata,sonata,Php,Symfony,Sonata Admin,Symfony Sonata,Sonata,我想在Sonata管理包中创建一个自定义页面小枝(例如克隆): 我使用本教程: 这是我的控制器CRUDController.php: <?php // src/AppBundle/Controller/CRUDController.php namespace AppBundle\Controller; use Sonata\AdminBundle\Controller\CRUDController as Controller; class CRUDController exten

我想在Sonata管理包中创建一个自定义页面小枝(例如克隆):

我使用本教程:

这是我的控制器
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()));
    }
}

我觉得您忘了以正确的方式为此页面配置管理员服务,请检查

原因sonata默认使用SonataAdmin:CRUD控制器,如果要覆盖该控制器,则应指定一个自定义控制器

#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

您忘记为控制器配置路由。 Sonata管理员必须了解您的新操作,以便为其生成路由。为此,您必须在管理类中配置
configureRoutes
方法:

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

    // ...

    /**
     * @param RouteCollection $collection
     */
     protected function configureRoutes(RouteCollection $collection)
     {
         $collection->add('clone', $this->getRouterIdParameter().'/clone');
     }
}
正如您所看到的,路由的名称与CRUDController中操作的名称匹配(但没有操作!)。
您有操作的名称:“cloneAction”,因此路由的名称为“clone”

将来,您可以在生成管理类时直接创建自定义管理控制器(非常典型)为什么不“复制”?斯塔斯卡克的回答有帮助吗?