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>
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
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, ), ), ); }
Add a "/" after '/src' to have NAMESPACE => DIR.'/src/'.NAMESPACE, instead of NAMESPACE => DIR.'/src'.NAMESPACE,
Had the same issue, my problem was that my folder called "controller" instead of "Controller"