traditional
traditional

Reputation: 983

ZF2: Zend framework 2.2 Controller not found error

I am trying to create new module in Zend Skeleton applictaion. I am following the Zend framework's getting started tutorial about Album module. Only difference is I am using Project name instead of Album. Here is my module structure.

module directory structure

The contents of module.config.php is as below.

<?php
namespace Project;

return array(
  'router' => array(
    'routes' => array(
        'project' => array(
            'type' => 'segment',
            'options' => array(
                'route' => '/project[/:action][/:id]',
                'contraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id' => '[0-9]+'
                ),
                'defaults' => array(
                    'controller' => __NAMESPACE__.'/Controller/'.__NAMESPACE__,
                    'action' => 'index'
                )
            )
        )
    )
  ),
  'controllers' => array(
    'invokables' => array(
        'Project/Controller/Project' => 'Project/Controller/ProjectController',
    )
  ),
  'view_manager' => array(
    'template_path_stack' => array(
        'project' => __DIR__ . '../view',
    )
  ),
);

Contents of Module.php is as below.

<?php
namespace Project;

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

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

The file autoload_classmap.php return only an empty array.

<?php
return array()

When I am trying to access the url http://local.zf2/project/, I get following error.

Fatal error: Class 'Project/Controller/ProjectController' not found in 
/home/bliss/public_html/local.zf2/vendor/zendframework/zendframework/library
/Zend/ServiceManager/AbstractPluginManager.php on line 170

When I looked at the AbstractPluginManager.php file the line 170 contains $instance = new $invokable();.

I dumped $invokable variable to see the value it has. It returned string(36) "Project/Controller/ProjectController"

Somehow ZF2 is not able to find that file. Can some one please help? Thanks.

EDIT: Here is the contents of ProjectController.php file. If it could find the file, it will die, showing the name of the file.

<?php
namespace Project\Controller;

die(__FILE__);

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

class ProjectController extends AbstractActionController
{
    public function indexAction()
    {
        return array();
//        return new ViewModel();
    }
}
?>

Upvotes: 1

Views: 8936

Answers (2)

user3955897
user3955897

Reputation: 1

Check your:

ProjectController.php

//must declare the class like below

class ProjectController extends AbstractActionController

Upvotes: 0

Tounu
Tounu

Reputation: 563

'controllers' => array(
  'invokables' => array(
      'Project/Controller/Project' => 'Project/Controller/ProjectController',
  )
),

You need to use backslashes on that one ! All the namespaces are declared with backslashes. That's probably why the AbstractPluginManager doesn't find your ProjectController ;).

'defaults' => array(
 'controller' => __NAMESPACE__.'/Controller/'.__NAMESPACE__,
'action' => 'index'
)

Same here

Upvotes: 1

Related Questions