Zend Framework 2: Uncaught Exception - Module can't be initialized - php

I'm new to using ZF2, and running into an issue getting the first project I need to work on set up locally. I've gone through and set up the application locally, but when I try to access the home page I receive the following exception error:
Fatal error: Uncaught exception
'Zend\ModuleManager\Exception\RuntimeException' with message 'Module
(Application) could not be initialized.' in
/var/www/myproject/vendor/ZF2/library/Zend/ModuleManager/ModuleManager.php
on line 140
Zend\ModuleManager\Exception\RuntimeException: Module (Application)
could not be initialized. in
/var/www/myproject/vendor/ZF2/library/Zend/ModuleManager/ModuleManager.php
on line 140
There is also some output being echoed from a Call Stack trace.. not sure if it will be helpful in resolving this:
getApplication()->getEventManager(); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->attach($eventManager); $this->initDatabase($e); } public function initDatabase($e) { Feature\GlobalAdapterFeature::setStaticAdapter($e->getApplication()->getServiceManager()->get('Zend\Db\Adapter\Adapter')); } public function getConfig() { return include __DIR__ . '/config/module.config.php'; } public function getServiceConfig() { return array( 'factories' => array( 'dbadapter' => new Zfe\Factory('db'), ), ); } public function getAutoloaderConfig() { return array( 'Zend\Loader\StandardAutoloader' => array( 'namespaces' => array( __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, ), ), ); } }
An image with the full stack trace and errors:
There was a very similar question already posted about this topic: Zend Framework 2 tutorial: Module (Application) could not be initialized .
Reading through that posting I followed the suggested answers recommendations of setting an absolute path for module_paths within application.config.php, however this did not affect my problem.
application.config.php excerpt:
'module_paths' => array(
__DIR__.'/../module',
'./vendor',
),
<?
namespace Application;
use Zend\Db\TableGateway\Feature;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Model;
use Zfe;
class Module implements ServiceProviderInterface {
public function onBootstrap(MvcEvent $e) {
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$this->initDatabase($e);
}
public function initDatabase($e) {
Feature\GlobalAdapterFeature::setStaticAdapter($e->getApplication()->getServiceManager()->get('Zend\Db\Adapter\Adapter'));
}
public function getConfig() {
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig() {
return array(
'factories' => array(
'dbadapter' => new Zfe\Factory('db'),
),
);
}
public function getAutoloaderConfig() {
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
Any insight as to where I could look to start debugging this would be much appreciated!

As per my comment, the error indicates ZF couldn't find the module class. In this case it is because a short open tag is being used (<? instead of <?php). PHP code being output is generally a good indicator of this.

Related

ZF2 - shared models between modules

In current state I've got two modules - main module, and admin panel module.
Main module is called "Kreator", admin -> "KreatorAdmin". All the models are located inside the Kreator module (Kreator/Model/UserTable.php etc.).
"KreatorAdmin" is almost empty, there is a configuration for it:
KreatorAdmin/config/module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'KreatorAdmin\Controller\Admin' => 'KreatorAdmin\Controller\AdminController',
),
),
'router' => array(
'routes' => array(
'zfcadmin' => array(
'options' => array(
'defaults' => array(
'controller' => 'KreatorAdmin\Controller\Admin',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view'
),
),
);
KreatorAdmin/src/KreatorAdmin/AdminController.php
<?php
namespace KreatorAdmin\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AdminController extends AbstractActionController
{
public function indexAction()
{
//$this->getServiceLocator()->get('Kreator\Model\UserTable');
return new ViewModel();
}
}
KreatorAdmin/Module.php
<?php
namespace KreatorAdmin;
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__,
),
),
);
}
}
Simply adding "use" statements in controller and navigating by namespaces results in error
Argument 1 passed to KreatorAdmin\Controller\AdminController::__construct() must be an instance of Kreator\Model\UserTable, none given,
I also tried to play a bit with service manager as described here:
ZF2 Models shared between Modules but no luck so far.
How am I supposed to access UserTable from KreatorAdmin/src/KreatorAdmin/AdminController.php ?
Cheers!
update 1
I've added getServiceConfig to Module.php
public function getServiceConfig()
{
return [
'factories' => [
// 'Kreator\Model\UserTable' => function($sm) {
// $tableGateway = $sm->get('UserTableGateway');
// $table = new UserTable($tableGateway);
// return $table;
// },
// 'UserTableGateway' => function($sm) {
// $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
// $resultSetPrototype = new ResultSet();
// $resultSetPrototype->setArrayObjectPrototype(new User());
// return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
// },
'DbAdapter' => function (ServiceManager $sm) {
$config = $sm->get('Config');
return new Adapter($config['db']);
},
'UserTable' => function (ServiceManager $sm) {
return new UserTable($sm->get('UserTableGateway'));
},
'UserTableGateway' => function (ServiceManager $sm) {
$dbAdapter = $sm->get('DbAdapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new User());
return new TableGateway('users', $dbAdapter, null, $resultSetPrototype);
},
],
];
}
And updated controller
class AdminController extends AbstractActionController
{
protected $userTable;
public function indexAction()
{
$userTable = $this->getServiceLocator()->get('Kreator\Model\UserTable');
return new ViewModel();
}
}
First error - using commented version:
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Zend\Db\Adapter\Adapter
Second - using uncommented part:
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Kreator\Model\UserTable
Solution
If anyone wonder. Using above configuration there is a correct solution in jobaer answer.
Using commented version, you have to remember to add
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
somewhere in config to service_manager.
May be you messed up with ZF2 and ZF3 configuration. I am not sure but somewhere may be, you tried to create a factory of AdminController by passing an instance of UserTable to make it available inside AdminController's action methods. And later you are not passing that instance of UserTable into the AdminController's constructor while working with it further. The highlighted part from the previous line results in that error.
In ZF2 you do not need to pass that UserTable instance in the controller's constructor for its availability. Just use the following one in any controller's action methods.
$userTable = $this->getServiceLocator()->get('UserTable');
If want to know how this process is done, please, refer to this part of the tutorial.

Zend framework fatal error in skelton aplication

Fatal error: Class 'Album\Album' not found in /var/www/html/zf2/module/Album/Module.php on line 43
this error is showing whenever i am trying ti access localhost/album
my module.php
<?php
namespace Album;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Album\Model\AlbumTable;
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';
}
public function getServiceConfig()
{
return array(
'factories' => array(
'Album\Model\AlbumTable' => function($sm) {
$tableGateway = $sm->get('AlbumTableGateway');
$table = new AlbumTable($tableGateway);
return $table;
},
'AlbumTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
),
);
}}?>
how to solve this error?
please give me the solution,zend is looking so tough i m trying to learn zend from 2 weeks bt this error is just eating my mind.
please help guyz
Make sure your namespaces are set up correctly. Your Album class has to be in the Album namespace if you're calling it like you are. Otherwise use the fully qualified name.

ZF2 custom view helper not registering

I'm new to ZF2 and are trying to create a custom view helper. In a view called profiles.phtml I do
echo $this->MyModuleHelper()->greetings('stack');
Which is resulting in
Fatal error: Class 'Dashboard\View\Helper\MyModuleHelper' not found in C:\dashboard\Application\module\Dashboard\Module.php on line 112
What am I missing and/or doing wrong?
Application/module/Dashboard/Module.php
namespace Dashboard;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\ModuleManager\Feature\ViewHelperProviderInterface;
use Dashboard\View\Helper\MyModuleHelper;
class Module implements ViewHelperProviderInterface {
public function onBootstrap(MvcEvent $e) {
//Some stuff
}
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__,
),
),
);
}
public function getViewHelperConfig() {
return array(
'factories' => array(
'MyModuleHelper' => function ( $sl ) {
return new MyModuleHelper(); //Line 112
}
),
);
}
}
Application/module/Dashboard/view/Helper/MyModuleHelper.php
namespace Dashboard\View\Helper;
use Zend\View\Helper\AbstractHelper;
class MyModuleHelper extends AbstractHelper {
public function __invoke() {
return $this;
}
public function greetings( $userName ) {
return $this->escapeHtml( sprintf("Hello, %s! ", $userName) );
}
}
Side note: I've also tried registering it in module.config.php (instead of Module.php) like
'view_helpers' => array(
'invokables' => array(
'MyModuleHelper' => 'Dashboard\View\Helper\MyModuleHelper',
)
)
As mentioned in the comments I had the directory structure messed up.
I placed my helper in
Application/module/Dashboard/view/Helper/MyModuleHelper.php
When it should have been put in
Application/module/Dashboard/src/Dashboard/View/Helper/MyModuleHelper.php

How to replace a ZF2 class with a custom one in Zend Framework?

In Zend Framework 2 it's quite easy to use a custom class instead of an invocable one from the framework. E.g. a ViewHelper:
namespace Application;
...
class Module {
public function onBootstrap(MvcEvent $mvcEvent) {
$application = $mvcEvent->getApplication();
$serviceManager = $application->getServiceManager();
$viewHelperManager = $serviceManager->get('ViewHelperManager');
$viewHelperManager->setInvokableClass('headmeta', 'MyNamespace\View\Helper\HeadMeta');
}
...
public function getAutoloaderConfig() {
return array(
...
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
'MyNamespace' => __DIR__ . '/../../vendor/MyNamespace/library/MyNamespace',
),
),
);
}
}
Now I'm having a problem with a bug in the Zend\Paginator\Adapter\DbSelect. It has already been fixed, but the fix has not been merged to the master branch yet. Anyway, I want to switch temporarily to my own DbSelect class. But DbSelect is not invocable. How to use a custom class insteead of a default framework class, e.g. Zend\Paginator\Adapter\DbSelect?
Paginator has its own adapter plugin manager. So you can push to him your own dbselect factory.

Zend Framework 2 - How to use an external library

I want to add my custom class "Authentication.php" to my project but I don't understand how I have to do it ?
I have read many howto about the external libs but nothing work.
ZendFramework/module/Firewall/Module.php
class Module
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
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__,
'MyNamespace' => __DIR__ . '/../../vendor/MyNamespace/lib/MyNamespace',
),
),
);
}
}
ZendFramework/vendor/MyNamespace/lib/MyNamespace
/Authentication.php
<?php
class Authentication {
public function test()
{
die('Works fine');
}
}
?>
How I can call my external lib in my controllers.
Thanks you very much !
I try like this:
1)
//module/Application/Module.php
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
'Mynamespace' => __DIR__ . '/../../vendor/Mynamespace',
),
),
);
}
2)
//vendor/Mynamespace/MyClass.php
namespace Mynamespace;
class MyClass
{
//...
}
3) I use it, for example in my controller:
use Zend\Mvc\Controller\AbstractActionController;
use Mynamespace\MyClass;
class AdminController extends AbstractActionController
{
public function indexAction()
{
$myclass = new MyClass();
}
}
For this kind of library, just type in your application.config.php
<?php
return array(
'modules' => array(
'ZendDeveloperTools',
'Application',
'YourLibrary' // <-- here
...
in composer.json file add the library as below
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": ">2.2.0rc1",
"doctrine/doctrine-orm-module": "0.7.*",
"zendframework/zend-developer-tools": "dev-master",
"twig/twig": ">=1.12.3",
}
Then in your application.config.php
under the modules array
'modules' => array(
'Application',
'ZendDeveloperTools',
'ZfcTwig',
'DoctrineModule',
'DoctrineORMModule','yourdir',
),
So do something similar to it.

Categories