phpsymfonysonata-adminsonata

Create a custom route in a custom page


I'm using Symfony 4.3 and Sonata 3.x version.

I'm trying to create a custom route in a custom Page but I get the error :

An exception has been thrown during the rendering of a template ("unable to find the route `admin.photocall|admin.photocall_gallery.moderate`")

I have an entity X with a OneToMany relation to the Y entity. Explanation with code :

class XAdmin extends AbstractAdmin
{  
    [...]
    protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
    {
        $admin = $this->isChild() ? $this->getParent() : $this;
        $id = $admin->getRequest()->get('id');

        if ($this->isGranted('LIST')) {
            $menu->addChild('Galerie', [
                'uri' => $admin->generateUrl('admin.photocall_gallery.list', ['id' => $id])
            ]);
        }
    }
}

Then there is my YAdmin :

class YAdmin extends AbstractAdmin
{
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper->add('_action', null, [
            'actions' => [
                'clone' => [
                    'template' => 'admin/PhotocallListAdmin/__list_action_moderate.html.twig'
                ]
            ]
        ])
        ;
    }

    protected function configureRoutes(RouteCollection $collection)
    {
        if ($this->isChild()) {
            $collection->clearExcept(['list', 'moderate']);
            $collection->add($collection->getBaseCodeRoute().'.moderate', 'moderate');
            return;
        }
    }
}

So there, I add an action with a template which look like this :

<a class="btn btn-sm" href="{{ admin.generateObjectUrl('moderate', object) }}">
{% if not object.ismoderate %}
    Moderate
{% else %}
    Undo moderation
{% endif%}
</a>

So the error says that it's unable to find the route admin.photocall|admin.photocall_gallery.moderate. But when I dump the $collection in YAdmin after adding the moderate route, I have two elements :

I searched but it looks like that nobody else did this.


Solution

  • I write Gaska's comment to close this issue.

    I just had to add :

    $collection->add('moderate', 'moderate'); and then clear the cache.

    Thanks to him