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
Had the same issue, my problem was that my folder called "controller" instead of "Controller"
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,
Related
I am using ZfcAdmin module for ZF2 (https://github.com/ZF-Commons/ZfcAdmin/) and I can't go through using my own controller.
According to the module documentation (https://github.com/ZF-Commons/ZfcAdmin/blob/master/docs/2.Routes.md) I should be able to use my own controller after simply adding this snippet from docs in my new module routes config, however it just cause a redirect to main page as it didn't find the route. So I've added this part:
'controllers' => array(
'invokables' => array(
'Admin\Controller\Admin' => 'Admin\Controller\AdminController',
),
),
Which results in error:
Exception: Zend\Mvc\Controller\ControllerManager::createFromInvokable: failed retrieving "admincontrolleradmin(alias: Admin/Controller/Admin)" via invokable class "Admin\Controller\AdminController"; class does not exist in /var/www/app/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php:240
Module.php
<?php
namespace Admin;
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(
'Admin\Controller\Admin' => 'Admin\Controller\AdminController',
),
),
'router' => array(
'routes' => array(
'zfcadmin' => array(
'options' => array(
'defaults' => array(
'controller' => 'Admin/Controller/Admin',
'action' => 'test',
),
),
),
),
),
);
module/Admin/src/Admin/AdminController.php
<?php
namespace Admin\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class AdminController extends AbstractActionController
{
public function testAction()
{
echo('test');
die();
}
}
I am 99% sure it's my fault, not zfcadmin itself. Even though, I have no idea where I made a mistake while creating my own controller which can't be found.
Zend 2 cannot find your "AdminController.php".
Your best option is to change "AdminController.php" to be under "module/Admin/src/Controller/", not "module/Admin/src/Admin/".
I'm trying to setup a module in the Zend Framework. Right now all I want is for it to go to my summary.phtml page, which will display Hello World.
I have setup the directory structure under my module directory as follows:
My files are as follows:
module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'BlindQC\Controller\BlindQC' => 'BlindQC\Controller\BlindQCController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'blinqc' => array(
'type' => 'Literal',
'options' => array(
'route' => '/summary',
'defaults' => array(
'__NAMESPACE__' => 'BlindQC\Controller',
'controller' => 'BlindQC',
'action' => 'summary',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'blindqc' => __DIR__ . '/../view',
),
),
);
BlindQCController.php
<?php
namespace BlindQC\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class BlindQCController extends AbstractActionController{
public function summaryAction(){
}
};
summary.phtml
<h1>Hello World</h1>
autoload_classmap.php
<?php
return array();
Module.php
<?php
namespace BlindQC;
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';
}
}
I also modified my project's application.config.php to include my module:
// This should be an array of module namespaces used in the application.
'modules' => array(
'Application',
'UIExperiment',
'Developer',
'User',
'Project',
'Report',
'ProjectFamily',
'FMEProcessManager',
'BlindQC'
),
When I try to go to the summary route (/blindqc/summary) I get a 404. Any idea what I'm doing wrong?
All I had to do was change the route in module.config.php to /blindqc/summary and rename view/blindqc/blindqc to view/blind-qc/blind-qc in my directory structure.
I added a module called Album. Here are my codes:
<AppName>/module/Album/Module.php
<?php
namespace Album;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements AutoloaderProviderInterface, ConfigProviderInterface
{
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';
}
}
<AppName>/module/Album/config/module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
Here is my controller:
<?php
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AlbumController extends AbstractActionController
{
public function indexAction()
{
}
public function addAction()
{
}
public function editAction()
{
}
public function deleteAction()
{
}
}
Then I added module name to config/application/config.php. Then I refreshed local server and I got the following errors and warnings:
Warning: include(/var/www/html/trialone/module/Album/config/module.config.php): failed to open stream: No such file or directory in /var/www/html/trialone/module/Album/Module.php on line 28
Warning: include(): Failed opening '/var/www/html/trialone/module/Album/config/module.config.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/html/trialone/module/Album/Module.php on line 28
Fatal error: Uncaught exception 'Zend\ModuleManager\Listener\Exception\InvalidArgumentException' with message 'Config being merged must be an array, implement the Traversable interface, or be an instance of Zend\Config\Config. boolean given.' in /var/www/html/trialone/vendor/zendframework/zendframework/library/Zend/ModuleManager/Listener/ConfigListener.php on line 342
Zend\ModuleManager\Listener\Exception\InvalidArgumentException: Config being merged must be an array, implement the Traversable interface, or be an instance of Zend\Config\Config. boolean given. in /var/www/html/trialone/vendor/zendframework/zendframework/library/Zend/ModuleManager/Listener/ConfigListener.php on line 342
How can I work around it?
App structure:
I've got a strange situation...
After the creation of the ZF2 SkeletonApplication I created an extra Module called Authentication with an AuthController and a LoginAction also in the view directory "authentication/auth" i placed a login.phtml.
When i run the app i get an error
Zend\View\Renderer\PhpRenderer::render: Unable to render template "authentication/auth/login"; resolver could not resolve to a file
The strange thing is that when I place the complete folder "authentication/auth/login.phtml" in the Standard Application Module View Folder it finds it.
So Zend is looking in the wrong directory.
This is my module.config.php (Authentication Module).
return array(
'router' => array(
'routes' => array(
'authentication' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/authentication/login',
'defaults' => array(
'controller' => 'Authentication\Controller\Auth',
'action' => 'login',
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'Authentication\Controller\Auth' => 'Authentication\Controller\AuthController',
),
),
'viewmanager' => array(
'template_path_stack' => array(
'authentication' => __DIR__ . '/../view',
),
)
);
This is the AuthController
namespace Authentication\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AuthController extends AbstractActionController
{
public function loginAction()
{
return new ViewModel();
}
}
I hope someone can point me in the right direction.
The complete path cannot be resolved by zf2. The viewManager is using your template pathStack to find the relative view. In your example, the viewManager is looking for this file :
DIR . '/../view/authentication/auth/login.phtml
In other way, you can add to your viewManager a templateMap like this :
'view_manager' => array(
'template_map' => array(
'authentication/auth/login' => __DIR__ . '/../view/where/you/want.phtml',
)
);
change your config:
'template_path_stack' => array(
'authentication' => __DIR__ . '/../view',
),
I am guessing you are a level too far back..
If you config is here:
Authentication/config/module.config.php
then you only want to go back a single level, then into your view directory. Your code would take you back a level higher, into the modules directory.
I'm newbie in Zend Framework 2. I created the folder structure and paste the code snippets from this page http://framework.zend.com/manual/2.0/en/user-guide/routing-and-controllers.html about routing in Zend Framework 2.
I get the following error:
( ! ) Fatal error: Class 'Album\Controller\AlbumController' not found in C:\wamp\www\zend\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php on line 178
Below is my classmap_autoload.php
<?php
return array();
Below is the Module.php
namespace Album;
class Module {
public function getAutoloaderConfig(){
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__.'/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespace' => array(
__NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__,
),
),
);
}
public function getConfig(){
return include __DIR__.'/config/module.config.php';
}
}
I have my AlbumController class already in the module/Album/
Here is my module.config.php from the Album module:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
AlbumCOntroller.php
<?php
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AlbumController extends AbstractActionController
{
public function indexAction()
{
}
public function addAction()
{
}
public function editAction()
{
}
public function deleteAction()
{
}
}
I have no idea why I have such an error.. I would like to learn what is happening inside Zend before rendering a page but before I intend to solve this problem ..
How can I fix this?
In Module.php 'namespace' => array must be namespaces (in plural)
Also you can compare your code with https://github.com/zendframework/zf2-tutorial
I am currently playing with the tutorial and integrated doctrine use. The above issue can show up with 'namespaces', probably after calling composer for an update, because ActionController's name seems to magically have been changed to AbstractActionController.
Without warning.
edit
I realized after inspection that the doctrine code was dated, so by pasting in a replacement, the controller name became out of date.