0

I'm trying to create a simple CRUD in Zf2 to get to know it and I'm having problems routing the only controller I have. I have this error;

"The requested controller could not be mapped to an existing controller class".

I'm trying to call this route : http://zf2.local/Listapp

This is my structure : module/Listapp/src/Listapp/Controller/ListappController.php

The namespace is namespace Listapp\Controller;

This is my autoloader config :

public function getAutoloaderConfig()
 {
     return array(
         'Zend\Loader\StandardAutoloader' => array(
             'namespaces' => array(
                 // Autoload Listapp classes
                 __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                 // Autoload ListappController classes
                 'ListappController' => __DIR__ . '/src/Listapp',
             )
         )
     );
 }

And this is my module.config.php :

return array(
 'controllers' => array(
     'invokables' => array(
         'Listapp\Controller\Listapp' => 'Listapp\Controller\ListappController'
     )
 ),

 'router' => array(
     'routes' => array(
         'listapp' => array(
             'type'    => 'segment',
             'options' => array(
                 'route'    => '/[:controller[/:action][/:id]]',
                 'constraints' => array(
                     'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                     'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                     'id'     => '[0-9]+',
                 ),
                 'defaults' => array(
                     'controller' => 'Listapp\Controller\Listapp',
                     'action'     => 'index',
                 ),
             ),
         ),
     ),
 ),

 'view_manager' => array(
     'template_path_stack' => array(
         'Listapp' => __DIR__ . '/../view',
     ),
 ), );

Any help would be appreciated thanks !

EDIT: This is the code in my controller (minus the other CRUD functions) :

namespace Listapp\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class ListappController extends AbstractActionController
 {
     public function indexAction()
     {
     }
 }
Tuck
  • 3
  • 3

1 Answers1

2

So just to further explain my comment, by including a :controller segment in your route, you've told ZF to try and match the first thing in your URL to something that the controller manager can load (in your case, one of the keys in you controller invokables). The controller default you defined in your route would only apply if you visited http://zf2.local/.

So for you, the quickest fix is to change your configuration to:

'controllers' => array(
     'invokables' => array(
         'Listapp' => 'Listapp\Controller\ListappController'
     )
 ),

'Listapp' in the URL will then match this controller, and everything will work as you expect.

In general it makes things clearer if you avoid using :controller in routes and have at least one route per controller instead, e.g.:

 'controllers' => array(
     'invokables' => array(
         'Listapp\Controller\Listapp' => 'Listapp\Controller\ListappController'
     )
 ),
 'router' => array(
     'routes' => array(
         'listapp' => array(
             'type'    => 'segment',
             'options' => array(
                 'route'    => '/listapp[/:action[/:id]]',
                 'constraints' => array(
                     'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                     'id'     => '[0-9]+',
                 ),
                 'defaults' => array(
                     'controller' => 'Listapp\Controller\Listapp',
                     'action'     => 'index',
                 ),
             ),
         ),
     ),
 ),
Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
  • After trying your example and removing the `:controller` I get this error : The requested URL could not be matched by routing. – Tuck Apr 24 '15 at 16:48
  • I did change the 'L' of listapp to be lower case in my example (IMO URLs should generally be lower case), so that may be the only issue. Try `http://zf2.local/listapp` – Tim Fountain Apr 24 '15 at 17:05
  • I agree with Tim Fountain about being more explicit with route definitions. The skeleton app suggests the magic route definition to try to reduce the number of things that need configuration for beginners, but I thing it has the affect of implying that magic routes are recommended. In fact, explicit routes are much easier to manage and debug. – dualmon Apr 25 '15 at 14:45