Search code examples
phprouteszend-framework2zend-route

Router in Zend Framework 2 : failed retrieving


I'm new with Zend Framework, i used to use framework CI and Laravel but in my project i should use ZF2. Honestly, i don't get it how to building app with ZF2. I followed the instructions from here : https://www.youtube.com/watch?v=OYkVHiXeGeY but i got some error.

Zend\Mvc\Controller\ControllerManager::createFromInvokable: failed retrieving "csnusercontrolleruser(alias: CsnUser\Controller\User)" via invokable class "CsnUser\Controller\UserController"; class does not exist

my strucktur folder:

:: module
   ->CsnUser
     >>config
       ->module.config.php
     >>src
       ->CsnUser
         ->Controller
           ->UserController.php
     >>view
       ->csn-user
         ->user
           ->index.phtml
     >>Module.php

Module.php

<?php 

namespace CsnUser;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module{

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespace' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }   

}

module.config.php

<?php 

return array(
    'controllers' => array(
        'invokables' => array(
            'CsnUser\Controller\User' => 'CsnUser\Controller\UserController',
        ),
    ),

    'router' => array(
        'routes' => array(
            'csn_user' => array(
                'type' => 'Literal',
                'options' => array(
                    'route' => '/csn-user',
                    'defaults' => array(
                        '__NAMESPACE__' => 'CsnUser\Controller',
                        'controller' => 'User',
                        'action' => 'index',
                    ),
                ),

                'may_terminate' => true,
                'child_routes' => array(
                    'default' => 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(),
                        ),
                    ),
                ),

            ),
        ),
    ),

    'view_manager' => array(
        /*'template_map' => array(
            'layout/Auth' => __DIR__ . '/../view/layout/xxxx.phtml',
        ),*/
        'template_path_stack' => array(
            'csn_user' => __DIR__ . '/../view'
        ),
    ),
);

UserController.php

<?php 

namespace CsnUser\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class UserController extends AbstractActionController{

    public function indexAction()
    {
        return new ViewModel();
    }
}

index.phtml

<h1>Welcome</h1>

Can anyone help me ?


Solution

  • Your getAutoLoaderConfig() method is incorrect.

    Try this once:

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

    Pay attention to the key namespaces in the array.

    In your instruction video at 4:40 they also use the plural :)