1
votes

I am trying to create HelloWorld Module using Zend Framework but its not working.I only gets this error:

Zend\Mvc\Controller\ControllerManager::createFromInvokable: failed retrieving "helloworldcontrollerindex(alias: HelloWorld\Controller\Index)" via invokable class "HelloWorld\Controller\IndexController"; class does not exist

Please Help me find why its being not working.

This Directory structure :

HelloWorld
  ->Module.php
  ->config
     ->Module.config.php
  ->src
     ->HelloWorld
   ->Controller
  ->view
    ->helloworld
       ->Controller
          ->index.phtml

Module.php

<?php
 namespace HelloWorld;
use HelloWorld\Controller;
use Zend\Mvc\ModuleRouteListner;
use Zend\Mvc\MvcEvent;

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



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

}
?>

module.config.php

<?php
   return array(
       'controllers' => array(
         'invokables' => array(
             'HelloWorld\Controller\Index'
                        => 'HelloWorld\Controller\IndexController',
          ),
    ),
'router' => array(
      'routes'      => array(
         'write-hello-world' => array(
             'type' => 'Zend\Mvc\Router\Http\Literal', 
             'options' => array(
               'route'   => '/writehello',
               'defaults' => array(
                  'controller' => 'HelloWorld\Controller\Index',
                  'action'     => 'index',
                ),
             ),
          ),
       ),
    ),

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

IndexController.php

<?php

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

class IndexController extends AbstarctActionController{

    public function indexAction ()
    {
        $views = new ViewModel(array('test' => 'hello bhavik'));
        $views->setTemplate('HelloWorld/index/index');
        return $views;
    }
}

?>

index.phtml

<h1>
    <?php
    echo $this->text;
    ?>

</h1>
4
Didnt't you forget use HelloWorld\Controller in the begining of Module.php ?franssu
oops ! I have edited it now but still its not working its giving me same errorBhavik Joshi
You have a forward slash instead of a backslash in use Zend/View\Model\ViewModel; in the controller. This might be causing a parse error. If that's not it - what's the full path to the location of IndexController.php?Tim Fountain
That is still not working... The path is module/HelloWorld/src/HelloWorld/IndexControllerBhavik Joshi

4 Answers

3
votes

This error basically means that ZF2 can't find your controller.

I believe this is a path issue: is your Controller directory a sub-directory of src->HelloWorld? Because it should:

Your_Module_Name
|- src 
   |- Your_Module_Name
      |- Controller
         |- IndexController.php

So, in your case:

HelloWorld
|- src
   |- HelloWorld
      |- Controller
         |- IndexController.php
2
votes

Had the same issue, my problem was that my folder called "controller" instead of "Controller"

1
votes

I think You made an error in getAutoloaderConfig() in "Module.php" file, you have to add a "s" to the key "namespace" under 'Zend\Loader\StandardAutoloader' key to have "namespaces" instead of "namespace" .

You should have at the end

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

0
votes

Add a "/" after '/src' to have NAMESPACE => DIR.'/src/'.NAMESPACE, instead of NAMESPACE => DIR.'/src'.NAMESPACE,