PHP/Zend: Fatal error - Class not found - zend-framework

I have a Zend project with the following directory structure:
/myApp
/application
Bootstrap.php
/configs
application.ini
/layouts
/modules
/default
/controllers
MessageController.php
ErrorController.php
IndexController.php
/models
/DbTable
Message.php
Message.php
MessageMapper.php
/views
Bootstrap.php
/login
/controllers
/forms
/library/login
/models
/plugins
/views
Bootstrap.php
/data/db
/library/zend
/public
http://localhost/ - works fine but i can not get the login module and the message inside the Default module to work. I get the following error:
Fatal error: Class 'Application_Module_Default_Model_MessageMapper' not found in /Library/WebServer/Documents/myApp/application/modules/default/controllers/MessageController.php on line 14
I am sure i am not initialising something i need to. Here is what my code looks like..
application.ini
autoloaderNamespaces[] ="Message_"
autoloaderNamespaces[] ="Login_"
autoloadernamespaces.0 = "Zend_"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.moduleControllerDirectoryName = "controllers"
resources.frontController.defaultControllerName = "index"
resources.frontController.defaultActionName = "index"
resources.frontController.params.displayExceptions = 0
resources.frontController.defaultModule = "default"
resources.frontController.params.prefixDefaultModule = "1"
resources.frontController.plugins[] = "Login_Plugin_SecurityCheck"
resources.modules = ""
/application/bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('HTML5');
}
}
/public/index.php
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Application.php';
require_once('Zend/Loader/Autoloader.php');
$autoloader = Zend_Loader_Autoloader::getInstance();
/module/default/controllers/MessageController.php
$message = new Application_Module_Default_Model_MessageMapper();
$this->view->entries = $message->fetchAll();
Let me know if any more details would help. Any help would be much appreciated.

Have you tried:
$message = new Default_Model_MessageMapper();
Also, are you autoloading your module in the module Bootstrap.php file? I'm not sure how much it has changed since I used ZF, but I had to include this:
$loader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Default_',
'basePath' => '/path/to/modules/default'
));

me too had the same issue, but my cause was very simple
i had a form Form_Template_Add & a controller TemplateController.
From the controller i called the form - new Form_Template_Add();
My form resides in a folder template.
Issue was with the folder name, see the 't' in template is small caps, while 'T' for controller & class name is caps.
I changes folder name to Template & it worked... hooya

Make sure you have created a Bootstrap.php in each module directory, which looks like:
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{}
In which Admin is the name of the module. Also, make sure there is actually a file Login.php inside directory modules/admin/forms with a class Admin_Form_Login
[Zend_Form][1.11.1] Error Message Fatal error: Class 'Admin_Form_Login' not found in ...\modules\default\controllers\IndexController.php

Related

module configuration in zend framework

I'm working with multi-modules CMS in Zend Framework. I want to create a structure like this:
testsite/
index.php
library/
zend/
test/
application/
configs/
application.ini
modules/
users/
controllers/
models/
....
frontend/
controllers/
models/
....
backend/
controllers/
models/
...
bootstrap.php
I have created the following code:
//in application.ini
resources.frontController.defaultModule = "frontend"
resources.frontController.defaultControllerName = "index"
resources.frontController.defaultAction = "index"
resources.frontController.plugins.init = "Test_Controller_Plugin_Initializer"
//in bootstrap.php
protected function _initControllers()
{
$this->bootstrap('frontController');
$this->_front = $this->getResource('frontController');
$this->_front->addControllerDirectory(APPLICATION_PATH . '/backend/controllers', 'backend');
$this->_front->addControllerDirectory(APPLICATION_PATH . '/frontend/controllers', 'frontend');
}
//in Test/controllers/plugin/initializer.php
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$front = Zend_Controller_Front::getInstance();
$request = $front->getRequest();
$request->setModuleName('frontend');
$request->setControllerName('index');
$request->setActionName('index');
}
It does not work when I go to my base URL .../testsite. Can you please let me know where I went wrong?
Have you tried putting your modules in the modules directory?
ie:
testsite/
index.php
library/
zend/
test/
application/
configs/
application.ini
modules/
users/
controllers/
models/
....
frontend/
controllers/
models/
....
backend/
controllers/
models/
...
bootstrap.php
Wich is typically where zf expects to find its modules. You might have to adjust you rapplication.ini settings to reflect this.
To make this work how you want it to refer to Zend_Application_Resource_Frontcontroller
specifically you need to pass an array of 'module' => 'controller' directory pairs to resources.frontController.controllerDirectory.
controllerDirectory: either a string value specifying a single
controller directory, or an array of module to controller directory
pairs.
I doubt this will solve all the problems but it should head you in the right direction.

zend 1.11.12 + modular structure + Class 'Users_Form_Login' not found

I'm making my first zend application, but I have problems with the autoload of modules.
At this time I load a form that I saved in the "forms" of the form "users", but I get a "Fatal Error".
This is my configuration:
application.ini:
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resouces.modules = ""
resources.frontController.params.displayExceptions = 1
;my library dir
autoLoaderNameSpaces.test = "Test_"
resources.view.helperPath.Zend_View_Helper = APPLICATION_PATH "/modules/default/views/helpers"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.layout.layout = layout
resources.view.doctype = "HTML5"
Dir tree:
application
-configs
-layouts
-modules
--default
--users
---controllers
----indexController.php -> class Users_IndexController extends Zend_Controller_Action
---forms
----Login.php -> class Users_Form_Login extends Zend_Form
---models
---views
---Bootstrap.php -> class Users_Bootstrap extends Zend_Application_Module_Bootstrap{}
--Bootstrap.php -> class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{}
.
.
.
within the indexAction() of the Users_IndexController I wrote:
$form = new Users_Form_Login();
And I get this error:
Fatal error: Class 'Users_Form_Login' not found in [...]/application/modules/users/controllers/IndexController.php on line 39
EDIT
Class content in complement for #Tim Fountain:
Bootstrap files:
In Bootstrap.php:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initActionHelpers ()
{
$config = $this->getOptions();
$acl = new Test_Acl($config['acl']);
$aclHelper = new Test_Controller_Action_Helper_Acl(null, array('acl'=>$acl));
Zend_Controller_Action_HelperBroker::addHelper($aclHelper);
}
}
In /Users/Bootstrap.php:
class Users_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
Each module has a bootstrap file. In the users/Bootstrap.php file have you decalred the namespace for the module?
/**
* Sets up the autoloading for this module. This function must be first in the bootstrap else other bootstrap functions might not work.
*/
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Users_',
'basePath' => APPLICATION_PATH . "/modules/users",
));
return $autoloader;
}
Your module bootstraps are not running because you have a typo in your config file:
resouces.modules = ""
should be
resources.modules = ""
then it should work.
Edit: In that case the first step is to see whether the bootstraps are being run. Edit your modules/users/Bootstrap.php class and temporarily add a method like this:
protected function _initTest()
{
echo "User bootstrap run";
exit;
}
reload the page in your browser and you should see that message if the bootstraps are being run. Remove it again after. If they are, then double check the filename and name of the form class (case sensitive).

module in zend framework not loading index controller

Following Pro Zend Framework techniques, I have created a module named 'Contact' in modules directory. Here's the directory structure.
|_application
|_Configs
|_application.ini
|_controllers
|_modules
|_Contact
|_Controllers
|_IndexController
|_models
|_views
Bootstrap.php
Bootstrap.php
The Bootstrap file in the application directory has an _initAutoload() function as shown below:
protected function _initAutoLoad(){
$autoLoader = Zend_Loader_Autoloader::getInstance();
$autoLoader->registerNamespace('CMS_');
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array('basePath' => APPLICATION_PATH , 'namespace' => '' ,
'resourceTypes' => array('form' => array('path' => 'forms/' , 'namespace' => 'Form_') ,
'model' => array('path' => 'models/' , 'namespace' => 'Model_'))));
return $autoLoader;
}
The Bootstrap file in the modules is:
class Contact_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAutoLoad(){
$autoloader=new Zend_Application_Module_Autoloader(array('namespace'=>'Contact_',
'basePath'=>dirname(__FILE__),));
return $autoLoader;
}
}
The application.ini file in the config folder has the following lines for setting up the module 'contact':
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
contact.resources.frontController.defaultControllerName = "index"
I have set up an application error controller which has a getmessage() function to display the error. When I try to load, http://localhost/zf_cms/public/contact, it gives an error:
getMessage() : Invalid controller specified (index)
The name of the index controller in modules->contact->controller is Contact_IndexController. I also created a view for index controller.
Please help me find the bug and let me know if I missed some info.
Thank you.
rename module and controller directory to lowercase . And it seems from your post that you are not adding .php extension to your IndexController do that aswell .

Zend Framework loads mappers from Application/Model/ instead of application/models

My zend app is created, everything seems to be in order but every time I try to do something like:
$accProducts = new Application_Models_AccProductsMapper();
Only get:
Warning: include_once(Application/Models/AccProductsMapper.php): failed to open stream: No such file or directory in /home/blah/blah/blah/Loader.php on line 148
however, the AccProductsMapper.php file do exist in such directory, directories within the zend app are all lowercase tough.
I've spend a lot of time looking for something to solve this issue with no good results at all.
Bootstrap.php
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
protected function _initAutoload()
{
$moduleLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH));
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace(array('App','My_'));
return $moduleLoader;
}
}
The standard Zend_Loader_Autoloader_Resource class added to each module looks for models with the class prefix <ModuleNamespace>_Model_ in <module-directory>/models.
For the default module, the namespace is defined in your config's appnamespace property (defaults to Application). The directory is typically application.
To summarize, create your default module model classes in application/models with class prefix Application_Model_, eg
<?php
// application/models/AccProductsMapper.php
class Application_Model_AccProductsMapper
{
// etc
As for your _initAutoload() method, I can't tell what you're doing with that module loader and would advise you don't need it at all. You can register PEAR style namespaces in your config file, eg
autoloadernamespaces.App = "App_"
autoloadernamespaces.My = "My_"

zend_form config problem

I have a structure like follows:
/application
.....
--/modules
----/structure
------/controllers
--------/indexController.php
------/forms
--------/Department.php //here class Structure_Form_Department extends Zend_Form
in indexController.php
...
public function saveAction()
{
$request = $this->getRequest();
$form = new Structure_Form_Department();//<-- error
....
}
and i get error
Fatal error: Class 'Structure_Form_Department' not found
when try to zf enable form module - receive :
An Error Has Occurred
This project already has forms enabled.
i think this is a config-like problem... but do not understand what i need to do...
EDIT 1
found good solution here
but for some way zend starts repeat executing _init... functions from default bootstrap.php....
I was also facing a similar problem few months ago and I got the solution by writing following code :
In application.ini
autoloadernamespaces[] = "Structure_"
In Bootstrap.php
protected function _initAutoload()
{
$autoloader=new Zend_Application_Module_Autoloader(array(
'namespace' => 'Structure',
'basePath' => dirname(__FILE__).DIRECTORY_SEPARATOR.'Structure'
));
}
And at the index.php
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH),
get_include_path(),
)));
Please let me know if it doesn't works.....
I guess adding Application in-front of Structure_Form_Department will work.
ie
Application_Structure_Form_Department()
Or you may want to tell in the config.ini the from appnamespace = "Application" to appnamespace = ''.
I have some piece of code in github. You can see how the modules work.
$contactForm = new Contact_Form_Contact();
Name of form is
class contact_Form_Contact extends Zend_Form
All codes over github. Check it out .
https://github.com/harikt/blog/blob/master/application/modules/contact/controllers/IndexController.php
https://github.com/harikt/blog/blob/master/application/modules/contact/forms/Contact.php