0

I am new in Symfony as well as sonata admin, I want to make a custom route with custom action in sonata admin.any one know how to make a custom route with custom action and how to call that custom route from twing file ?.

Sandeep Gajera
  • 229
  • 4
  • 14

1 Answers1

0

Example of route in your AdminBundle:

<route id="get_driver_location" path="/driver-location">
    <default key="_controller">MyAdminBundle:Core:getlocation</default>
</route>

Then you must create a controller called CoreController in the controller directory of your bundle

    namespace My\AdminBundle\Controller;

    use Symfony\Component\Config\Definition\Exception\Exception;
    use Symfony\Component\HttpFoundation\Response;
    use Sonata\AdminBundle\Controller\CoreController as BaseController;

    class CoreController extends BaseController {

    public function getlocationAction() {
        //your logic here
       ......
       return $this->render($this->getAdminPool()->getTemplate('driver'), array(
                    'base_template' => $this->getBaseTemplate());
    }

    }

Edit: Add Twig:

-Resources

  -views

    -Core

      -driver.html.twig

in the file driver.html.twig you make a route

<div>
                <a class="btn btn-default" href="{{ path('get_driver_location') }}" role="button">
                    {{ "example"|trans({}, "MyAdminBundle") }}
                </a>
            </div>
Mohamed Ben HEnda
  • 2,686
  • 1
  • 30
  • 44
  • how to cal above route from view twig file ? – Sandeep Gajera Nov 13 '17 at 12:16
  • It give me below error. Error: "The controller for URI \"\/admin\/department_details\/4614\" is not callable. Expected method \"retrieveDepartmentDetailsAction\......" – Sandeep Gajera Nov 13 '17 at 12:26
  • Please, when you have an error, edit you post that contains all necessary informations about the error. – Mohamed Ben HEnda Nov 13 '17 at 12:29
  • 1 ) I add Below code in vendor\sonata-project\admin-bundle\Resources\config\routing\sonata_admin.xml file AppBundle:CRUD:retrieveDepartmentDetailsAction 2)Call Route from Twig file
    3)Make a Function in CRUD Controller in appbundle folder
    – Sandeep Gajera Nov 13 '17 at 12:47
  • I Set above code and It give me Error below: There is no `_sonata_admin` defined for the controller `AppBundle\\Controller\\CRUDController` and the current route `get_department_details`" – Sandeep Gajera Nov 13 '17 at 12:49
  • no, never change the code in the vendor. you should add a new file called sonata_admin.xml in your bundle which will be contain the new route – Mohamed Ben HEnda Nov 13 '17 at 12:49
  • ok let me change it but one question is in which folder i make a sonata_admin.xml file ? – Sandeep Gajera Nov 13 '17 at 12:57
  • I make a sonata_admin.xml file in below folder of my bunddle D:\xampp\htdocs\meopin_2\trunk\src\AppBundle\Resources\config\routing\ But it give me error Below An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "get_department_details" as such route does not exist."). – Sandeep Gajera Nov 13 '17 at 13:02
  • please help me something is missing or wrong from my side , please suggest me – Sandeep Gajera Nov 13 '17 at 13:08
  • thanks for help it is done few change in your code and it will be done – Sandeep Gajera Nov 14 '17 at 13:37
  • and I also Folow https://groups.google.com/forum/#!topic/sonata-devs/8GK0kpa2cik link – Sandeep Gajera Nov 14 '17 at 13:38