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/".
Related
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'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 ?
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 :)
I solved the problem by adding a string to composer.json:
"autoload": {
"psr-0": {
"MyModule": "module/MyModule/src/"
}
}
and then, in the console, I navigated to the project folder and ran:
php composer.phar update
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 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,
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.