Hamza Najemi
Hamza Najemi

Reputation: 243

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 ) :

enter image description here

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 :

enter image description here

can you help me ..?

Upvotes: 2

Views: 4975

Answers (3)

Oleh Diachenko
Oleh Diachenko

Reputation: 662

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

Upvotes: 0

staskrak
staskrak

Reputation: 863

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".

Upvotes: 1

slavchoo
slavchoo

Reputation: 81

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

Upvotes: 3

Related Questions