There is no `_sonata_admin` defined for the controller `***\UserController` and the current route `resetPassword`

4,261 views
Skip to first unread message

Jessica Mauerhan

unread,
Nov 29, 2013, 10:10:22 AM11/29/13
to sonat...@googlegroups.com
I followed the documentation for adding a custom route. When I click on the button in the list view, I get the error: 
There is no `_sonata_admin` defined for the controller `***\UserController` and the current route `resetPassword`


My Admin Service is defined as follows:

  sonata.admin.user:
      class: ****\Admin\UserAdmin #User Admin
      tags:
          - { name: sonata.admin, model_manager: cems_model_manager, manager_type: orm, group: core, label: "Users" }
      arguments:
          - ~
          - models\User #User Model
          - '****Bundle:User' #User Controller
      calls:
          - [ setTranslationDomain, [****Bundle]]


I have added my route in my UserAdmin
    protected function configureRoutes(RouteCollection $collection)
    {
        parent::configureRoutes($collection);
        $collection->add('password_reset', $this->getRouterIdParameter() . '/resetPassword/');
    }

And created the custom controller and action.

<?php

namespace ****\Controller;

use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;

class UserController extends CRUDController
{

    /**
     * @Route("/User/{userId}/resetPassword/", name="resetPassword")
     */
    public function resetPasswordAction(Request $request, $userId)
    {
        // code here 
    } 
}

The route works fine if I browse to it manually or via an ajax call, which our application does at another point.  But when I try to click on the button in the list view, which is generated in the configureListFields() in UserAdmin, I get the subject error. I can't figure out how any of the other default actions put the code into the request, they look the same as this one does. I found a few other people asking this question when I googled the error but my admin appears to be configured correctly, and that was the only answer I could find - and it seems outdated as the arguments appear to be in different orders now. 

Thanks in advance for any help you guys can provide.

- Jessica

Cassiano Tartari

unread,
Nov 29, 2013, 10:37:43 AM11/29/13
to sonata-devs
Did you create the template for the custom route? Looking quickly to your code seems to be all right.

     /**
      * {@inheritdoc}
      */
     protected function configureListFields(ListMapper $listMapper) {
       
          $listMapper
                  ->add('_action', 'actions', array(
                      'label' => 'Ações',
                      'actions' => array(
                          'password_
reset' => array('template' => '****Bundle:CRUD:list__action_password_reset.html.twig'),
                      )
                  ))
          ;
     }



{# ****Bundle:CRUD:list__action_password_reset.html.twig #}
{% if admin.hasRoute('password_reset') %}
    <a href="{{ admin.generateObjectUrl('password_reset', object) }}" class="btn" title="{{ 'action_password_reset'|trans({}, 'SonataAdminBundle') }}">
        <i class="icon-share"></i>
        {{ 'action_password_reset'|trans({}, 'SonataAdminBundle') }}
    </a>
{% endif %}

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


--
You received this message because you are subscribed to the Google Groups "sonata-devs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonata-devs...@googlegroups.com.
To post to this group, send email to sonat...@googlegroups.com.
Visit this group at http://groups.google.com/group/sonata-devs.
For more options, visit https://groups.google.com/groups/opt_out.

Jessica Mauerhan

unread,
Nov 29, 2013, 11:47:15 AM11/29/13
to sonat...@googlegroups.com
yes, there is a template.

Jessica Mauerhan

unread,
Nov 29, 2013, 11:49:09 AM11/29/13
to sonat...@googlegroups.com
protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
                ->addIdentifier('firstname', null, ['sortable' => true, 'label' => 'First Name'])
                ->addIdentifier('lastname', null, ['sortable' => true, 'label' => 'Last Name'])
                ->addIdentifier('username', null, ['sortable' => true, 'label' => 'Username'])
                ->add('_action', 'actions',
                        [
                    'actions' => [
                        'password_reset' => ['template' => '****Bundle:Admin:list__action_generic.html.twig', 'label' => 'action_password_reset', 'route' => 'password_reset'],
                    ]
                        ]
                )
        ;
    }

Jessica Mauerhan

unread,
Nov 29, 2013, 11:50:51 AM11/29/13
to sonat...@googlegroups.com
Sorry for so many replies, here is the template.

{% if admin.hasRoute(actions.route) %}
<a href="{{ admin.generateObjectUrl(actions.route, object) }}" class="btn btn-small view_link" title="{{ actions.label|trans }}">
        {{ actions.label|trans }}
    </a>
{% endif %}


But the button shows up fine, it's when I click it and go to the route. I copied it from the edit action button basically. 
{% if admin.isGranted('EDIT', object) and admin.hasRoute('edit') %}
    <a href="{{ admin.generateObjectUrl('edit', object) }}" class="btn edit_link btn-small" title="{{ 'action_edit'|trans({}, 'SonataAdminBundle') }}">
        <i class="icon-edit"></i>
        {{ 'action_edit'|trans({}, 'SonataAdminBundle') }}
    </a>
{% endif %}



On Friday, November 29, 2013 10:47:15 AM UTC-6, Jessica Mauerhan wrote:

Jessica Mauerhan

unread,
Nov 29, 2013, 12:00:00 PM11/29/13
to sonat...@googlegroups.com
I figured it out. I had to remove my Route definition from the action, and switch the route in configure route to resetPassword. The new code looks like this:

User Admin:
    protected function configureRoutes(RouteCollection $collection)
    {
        parent::configureRoutes($collection);
        $collection->add('resetPassword', $this->getRouterIdParameter() . '/resetPassword');
    }

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
                ->addIdentifier('firstname', null, ['sortable' => true, 'label' => 'First Name'])
                ->addIdentifier('lastname', null, ['sortable' => true, 'label' => 'Last Name'])
                ->addIdentifier('username', null, ['sortable' => true, 'label' => 'Username'])
                ->add('_action', 'actions',
                        [
                    'actions' => [
                        'resetPassword' => ['template' => 'CemsCoreBundle:Admin:list__action_generic.html.twig', 'label' => 'action_password_reset', 'route' => 'resetPassword'],
                    ]
                        ]
                )
        ;
    }


UserController
<?php

namespace Cems\CoreBundle\Controller;

use Sonata\AdminBundle\Controller\CRUDController;

class UserController extends CRUDController
{

    public function resetPasswordAction($id = null)
    {
        $id = $this->get('request')->get($this->admin->getIdParameter());
        $user = $this->admin->getObject($id);
    }
}


I copied the editAction to figure it out. 

I'll have to make a second action for my other code that calls it via ajax to use I think, but this part works now, so I'm happy.

Cassiano Tartari

unread,
Nov 29, 2013, 12:48:33 PM11/29/13
to sonata-devs
Good to know that is working now.

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


--
Reply all
Reply to author
Forward
Message has been deleted
0 new messages