Child Admin route is not being generated - Sonata Admin Bundle
Asked Answered
P

3

6

I'm trying to set up an Admin as a child of an other Admin in Sonata Admin Bundle.

I have 2 Admin classes:

  • CategoryAdmin
    This class contains the following method

    protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
    {            
         $id = $this->getRequest()->get('id');
    
         $menu->addChild(
             $this->trans('Projects'),
             array('uri' => $this->getChild('sonata.admin.project')->generateUrl('list', array('id' => $id)))
         );
    }
    
  • ProjectAdmin
    This class contains protected $parentAssociationMapping = 'category';
    category is the property in the model class representing the ManyToOne association.

I added the following lines to my service configuration for CategoryAdmin

calls:
    - [ addChild, ["@sonata.admin.project"]]

The routes for the child Admin are not being generated with this configuration. The link in the SideMenu (top menu) points to /admin/project/list?childId=1&id=1

Here is the output of the children of CategoryAdmin with dump()

array:1 [▼
    "sonata.admin.project" => ProjectAdmin {#406 ▶}
]

This means that the configuration for my child admin seems to be correct. I have no idea, why the routes for the child admin are not being generated.

I hope somebody can give me a hint, what the problem could be.

Periphrastic answered 20/2, 2015 at 18:55 Comment(0)
C
1

Do you have the $baseRouteName and $baseRoutePattern overriden in your admin class ?

If you do, Sonata will generate both child and parent routes with the same name resulting in the parent routes overriding the child ones.

Cliffhanger answered 6/7, 2015 at 15:54 Comment(2)
Is there no way around this? I have to define $baseRouteName and $baseRoutePattern because otherwise Sonata balks at my vendorless bundle (e.g. src/AppBundle/ )Sizzle
If using the dev-master version of the SonataAdminBundle is not a problem, the bug was solved in it (github.com/sonata-project/SonataAdminBundle/pull/3250). If not you can override the getBaseRoutePattern and getBaseRouteName to fix the behavior.Cliffhanger
O
4

Note for next gen sonata coders: If your route is not being generated, first check you didn't do:

protected function configureRoutes(RouteCollection $collection)
{
    //clear all routes except given !!!
    $collection->clearExcept(array('list', 'show'));
}

It costs me two days...

Owen answered 6/11, 2016 at 9:42 Comment(2)
Probably just saved me a couple of days!Ephemera
it cost me 2 hours woowwww thanks it clear child route too :'(Maledict
C
1

Do you have the $baseRouteName and $baseRoutePattern overriden in your admin class ?

If you do, Sonata will generate both child and parent routes with the same name resulting in the parent routes overriding the child ones.

Cliffhanger answered 6/7, 2015 at 15:54 Comment(2)
Is there no way around this? I have to define $baseRouteName and $baseRoutePattern because otherwise Sonata balks at my vendorless bundle (e.g. src/AppBundle/ )Sizzle
If using the dev-master version of the SonataAdminBundle is not a problem, the bug was solved in it (github.com/sonata-project/SonataAdminBundle/pull/3250). If not you can override the getBaseRoutePattern and getBaseRouteName to fix the behavior.Cliffhanger
B
1

I bumped into this issue while solving the problem for myself and decided to share the solution, which costed me several debugging hours...

The only way to generate a proper uri in this case is to use low-level routeGenerator which doesn't make any sonata suggestions, made inside generateMenuUrl method.

First you have to debug the routes, you have in your app (including autogenerated by sonata).

php bin/console debug:router

For example I have 3 nesting levels

hall -> seats scheme -> sector

And my routes are following:

  adminHall_list                             ANY      ANY      ANY    /admin/hall/list
  adminHall_create                           ANY      ANY      ANY    /admin/hall/create
  adminHall_edit                             ANY      ANY      ANY    /admin/hall/{id}/edit
  adminHall_delete                           ANY      ANY      ANY    /admin/hall/{id}/delete
  adminHall_adminScheme_list                 ANY      ANY      ANY    /admin/hall/{id}/scheme/list
  adminHall_adminScheme_create               ANY      ANY      ANY    /admin/hall/{id}/scheme/create
  adminHall_adminScheme_edit                 ANY      ANY      ANY    /admin/hall/{id}/scheme/{childId}/edit
  adminHall_adminScheme_delete               ANY      ANY      ANY    /admin/hall/{id}/scheme/{childId}/delete
  adminHall_adminScheme_adminSector_list     ANY      ANY      ANY    /admin/hall/{id}/scheme/{childId}/sector/list
  adminHall_adminScheme_adminSector_create   ANY      ANY      ANY    /admin/hall/{id}/scheme/{childId}/sector/create
  adminHall_adminScheme_adminSector_edit     ANY      ANY      ANY    /admin/hall/{id}/scheme/{childId}/sector/{childChildId}/edit
  adminHall_adminScheme_adminSector_delete   ANY      ANY      ANY    /admin/hall/{id}/scheme/{childId}/sector/{childChildId}/delete

In admin classes baseRouteName and baseRoutePattern has been overridden.

// HallSchemeAdmin.php
$this->baseRouteName = 'adminScheme';
$this->baseRoutePattern = 'scheme';

To generate a most deep listing url:

$url = $admin->getRouteGenerator()->generate('adminHall_adminScheme_adminSector_list', [
    'id' => $admin->getRequest()->get('id'),
    'childId' => 555, // put required id
]);

It will produce the url like this:

/admin/hall/495/scheme/555/sector/list

If you need edit url, you have to provide childChildId param too:

$url = $admin->getRouteGenerator()->generate('adminHall_adminScheme_adminSector_edit', [
    'id' => $admin->getRequest()->get('id'),
    'childId' => 555, 
    'childChildId' => 12345 
]);

The result is: /admin/hall/495/scheme/555/sector/12345/edit

Brumfield answered 11/1, 2021 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.