Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override bundle action

I have a project with symfony 2 and im using a SonataAdminBundle for my backend. How can i override a dashboardAction() to extend them for more features ?

like image 705
Pawel Avatar asked Feb 22 '23 23:02

Pawel


1 Answers

The routing configuration for this sonata admin can be found in

// vendor/bundles/Sonata/AdminBundle/Resources/config/routing/sonata_admin.xml
    <route id="sonata_admin_dashboard" pattern="/dashboard">
        <default key="_controller">SonataAdminBundle:Core:dashboard</default>
    </route>

Let say you have a bundle named 'My/AdminBundle' that holds the controller that should extend the dashboardActions. Then try the following:

  1. Create a controller in /My/AdminBundle/Controller/CoreController.php

    namespace My\AdminBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    use Symfony\Component\HttpFoundation\Response;
    use Sonata\AdminBundle\Controller\CoreController as BaseCoreContBroller;
    
    class CoreController extends BaseCoreContBroller
    {
    
        public function dashboardAction()
        {
            // your custom code
    
            // call parent method
            parent::dashboardAction();
        }
    }
    
  2. Open the bundle routing configuration file located /My/AdminBundle/Resources/config/routing.yml (you might have different configuration format such as xml)

sonata_admin_dashboard:
     pattern:  /dashboard
     defaults: { _controller: MyAdminBundle:Core:dashboard }
  1. Open the application routing configuration file and add the following after sonata configuration so that it will override it
admin:
    resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix: /admin

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

MyAdminBundle:
    resource: "@MyAdminBundle/Resources/config/routing.yml"
    prefix:   /admin

Disclaimer just so you know I have not used this in a project. I just check it locally and it worked. It is possible that this is not the best solution!

Hope this helps

like image 181
satrun77 Avatar answered Mar 08 '23 01:03

satrun77