Troubleshooting “The controller for URI is not cal

2020-06-30 04:37发布

I'm working on Symfony 2.3 and I declared a new route and new controller, but when I call this controller from the browser I get this error:

The controller for URI "/user/1" is not callable. in /dev.mydomain.org/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php at line 82

This is my simple route configuration:

user_homepage:
    pattern:  /user
    defaults: { _controller: UserBundle:Default:index }

user_show:
    pattern:  /user/{id}
    defaults: { _controller: UserBundle:Default:show }
    requirements:
        id:  \d+

And this is my very simple controller:

public function showUserAction($id)
{        
    return $this->render('UserBundle:Default:show.html.twig', array());
}

What is wrong?

9条回答
一纸荒年 Trace。
2楼-- · 2020-06-30 05:10

In my case, i was using symfony 2.

Prior version of symfony maintain method naming convention. Method suffix should contain Action word.

example:

in route yml file the method definition was

docudex_report_make_authorization:
pattern:  /make-authorization
defaults: { _controller: DocudexReportBundle:Default:makeAuthorization }

and in the controller the method was

public function makeAuthorization(){}

therefore i was getting the error.

After changing the method name to public function makeAuthorizationAction it worked perfectly.

查看更多
甜甜的少女心
3楼-- · 2020-06-30 05:14

You're defining your controller function as showUserAction while in the definition your saying it is show[Action].

Either change your route configuration

user_show:
    pattern:  /user/{id}
    defaults: { _controller: UserBundle:Default:showUser }
    requirements:
        id:  \d+

or change your controller signature

public function showAction($id)
{

See if this helps

查看更多
太酷不给撩
4楼-- · 2020-06-30 05:17

The logical name UserBundle:Default:show refers to UserBunde\Controller\DefaultController::showAction you have a method called showUserAction.

Either change the method name to showAction or change the logical name to UserBundle:Default:showUser.

查看更多
Viruses.
5楼-- · 2020-06-30 05:17

After big search, this worked for me:

1.- Create CRUDController

// src/Acme/DemoBundle/Controller/CRUDController.php

namespace Acme\DemoBundle\Controller;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Inter\PimeBundle\Entity\Empresa;

class CRUDController extends Controller
{
    public function publicarAction($id)
    {
       //code here...
    }
}

2.- Create the service

# app/config/config.yml
services:
    ebtity.admin.service:
        class: Acme\DemoBundle\Admin\EntityAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: group, label: label }
        arguments:
            - NULL
            - Acme\DemoBundle\Entity\Entity
            - AcmeDemoBundle:EntityAdmin

3.- Create the template for the action button

{# src/Acme/DemoBundle/Resources/views/CRUD/list__action_publicar.html.twig #}

<a class="btn btn-sm" href="{{ admin.generateObjectUrl('publicar', object) }}">Publicar</a>

4.- Configure route

// src/Acme/DemoBundle/Admin/EntityAdmin.php

namespace Acme\DemoBundle\Admin;

// ...

use Sonata\AdminBundle\Route\RouteCollection;

class EntityAdmin extends Admin
{
     // ...

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('name')
            ->add('engine')
            ->add('rescueEngine')
            ->add('createdAt')
            ->add('_action', 'actions', array(
                'actions' => array(
                    'publicar' => array(
                        'template' => 'AcmeDemoBundle:CRUD:list__action_publicar.html.twig'
                    )
                )
            ));
    }

    protected function configureRoutes(RouteCollection $collection)
    {
        $collection
                ->add('publicar', 
                    $this->getRouterIdParameter().'/publicar',
                    array('_controller' => 'AcmeDemoBundle:CRUD:publicar')    
                );
    }
}

5.- Clear cache

Hope it helps

查看更多
太酷不给撩
6楼-- · 2020-06-30 05:17

One of the reasons for this error is that you missed to add the word "Action" after the controller's method name. If you think that your routing is OK, check the method name.

查看更多
够拽才男人
7楼-- · 2020-06-30 05:22

Although not relevant to the example given, this error can also be caused if the controller Action is not public

查看更多
登录 后发表回答