Search code examples
phpsymfonydoctrine-ormsonata-adminsymfony-sonata

How add new method in Sonata CRUD controller?


In CRUDController of SonataBundle are a lot of actions, for example listAction(), editAction() etc. For user managment is link:

http://site.local/app_dev.php/admin/sonata/user/user/list

and route for this:

admin_sonata_user_user_list       -  /admin/sonata/user/user/list

I would like add own action for this controller: listSecondAction();

I copy method listAction and change name to listSecondAction. Next i open

http://site.local/app_dev.php/admin/sonata/user/user/listSecond

and i have error:

No route found for "GET /admin/sonata/user/user/listSecond"

So how can i add and where routing for this action? How should i enter to this routing?


Solution

  • In your admin class you have to add configureRoutes method :

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

    And in your controller you need to add this two actions :

    public function listSecondAction() {
        // Your code here
    }
    
    public function anotherAction($id = null) {
        // Here how to get the current object 
        $id = $this->get('request')->get($this->admin->getIdParameter());
        $object = $this->admin->getObject($id);
        // Your code here
    }
    

    Hope this helps