Ok, this is driving me nuts!
I have a directory structure as follows:
application
- modules
-- default
--- controllers
--- models
---- DbTable
---- Cachmapper.php
--- views
My config file looks like this
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] =
The application seems to work, if I navigate to localhost, it correctly goes to the index controller. But, for some reason it refuses to load any models.
Fatal error: Class 'Model_Cachmapper' not found in .............................../application/modules/default/controllers/IndexController.php on line 26
Ideas?
Thanks
Here's a working version (one of them, at least), for ZF 1.10.x and probably earlier, too.
index.php
<?php
// Define path to application directory
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
application.ini - the relevant parts
autoloadernamespaces[] = Zend_
includePaths.library = APPLICATION_PATH "/../library"
; Where will Zend_Application find the Bootstrap file
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
; Where are all the modules
resources.frontcontroller.moduledirectory = APPLICATION_PATH"/modules"
resources.modules[] = ""
; And which is the default module
resources.frontcontroller.defaultmodule = "default"
and in application/Bootstrap.php
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
/**
* Autoloader for the "public" module
*
* #return Zend_Application_Module_Autoloader
*/
public function _initPublicAutoload()
{
$moduleLoader = new Zend_Application_Module_Autoloader(
array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/modules/default'
)
);
return $moduleLoader;
}
}
I am a novice ZF user, so this may not be 'correct' but I have had no problems using DB Models this way.
My directory structure looks like this:
/
--/application
----/configs
----/controllers
----/forms
----/layouts
----/models
------/DbTable
In my application/public/index.php I have the following code:
...
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Application_');
require_once 'Zend/Loader/Autoloader/Resource.php';
$resources = new Zend_Loader_Autoloader_Resource(array(
'namespace' => 'Application',
'basePath' => APPLICATION_PATH
));
$resources->addResourceType('form','forms','Form');
$resources->addResourceType('model','models','Model');
$resources->addResourceType('dbtable','models/DbTable','Model_DbTable');
...
My application namespace is Application (if you couldn't tell).
My database models look like this (located in application/models/DbTable:
class Application_Model_DbTable_User extends Zend_Db_Table_Abstract
{
protected $_name = 'User';
protected $_primary = 'username';
}
class Application_Model_DbTable_WinLossTieScore extends Zend_Db_Table_Abstract
{
protected $_name = 'WinLossTieScore';
protected $_primary = 'id';
...
}
Also, the relevant portions of application/configs/application.ini:
appnamespace = "Application"
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
I would add a bootstrap class to the module as otherwise the module loader doesn't know about it.
class Default_Bootstrap extens Zend_Application_Module_Bootstrap
{
}
And store in applications/default/Bootstrap.php
http://akrabat.com/modules has some more info.
Add your models directory to your includePaths
I have:
includePaths.models = APPLICATION_PATH "/models"
I know, your models lies under your modules, but should be possible to do somehow. Maybe in the bootstrap instead of application.ini
Att.: Rob
Hi Rob,
Well i'm confused. Because i actually bought your book 'Zend Framework In Action'.
And in the bootstrap of the 'Places' app in the sourcecode, you have the following.
set_include_path(get_include_path()
. PATH_SEPARATOR . ROOT_DIR . '/library/'
. PATH_SEPARATOR . ROOT_DIR . '/application/models/'
. PATH_SEPARATOR . ROOT_DIR . '/application/forms/'
);
include 'Zend/Loader.php';
Zend_Loader::registerAutoload();
I know that the source is written for an older ZF version. My current app i'm working on is based on Zend_Application, and if i remove the models folder from my includePaths, the loader can't find my models.
Warning: include_once(Users.php) [function.include-once]: failed to open stream: No such file or directory in C:\Programmer\wamp\include\Zend\Loader.php on line 146
Related
Hi everyone i'm trying to create a rest service using the zend framework
I have basicly copied the actual app that this rest service is going to be for.
i removed the layout folder and the view folders.
My problem is the modules don't seem to be loading at all and i get the following output when i go to the index.php
problemInvalid controller specified (error)#0 /home/********/*****/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 /home/********/*****/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch() #2 /home/********/*****/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run() #3 /home/********/public_html/portal/websvc/index.php(34): Zend_Application->run() #4 {main}
I'm not sure why this isnt working. its basicly the same settings as my original app that has 20 modules and there is no problem with that.
PLEASE HELP! :'(
so i basicly have the following folder structure
/app/modules
/app/modules/v1
/app/modules/v1/controllers
/app/modules/v1/controllers/OutboundController.php
/app/modules/v1/controllers/ProductController.php
/app/modules/v1/Bootstrap.php
/app/modules/default
/app/modules/default/controllers
/app/modules/default/controllers/errorController.php
/app/config/
This is my application.ini
[production]
includePaths.library = APPLICATION_PATH "/../../../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.defaultModule = "v1"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.moduleControllerDirectoryName = "controllers"
resources.router.routes.rest.type = Zend_Rest_Route
My bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initautoloader() {
$autoloader = new Zend_Application_Module_Autoloader(array(
'basePath' => APPLICATION_PATH."/modules/default",
'namespace' => '',
));
$resourceAutoLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
));
$resourceAutoLoader->addResourceTypes(array(
"class" => array(
'namespace' => 'Class',
'path' => '/../../../library/Class'
),
"model" => array(
'namespace' => 'Model',
'path' => '/../../../library/Models'
)
));
}
protected function _initFrontModules()
{
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$front->addModuleDirectory(APPLICATION_PATH . '/modules');
}
}
My index.php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../../ibwms/applications/external/websvc'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../../../library'),
get_include_path(),
)));
/* Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
try{
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
Zend_Controller_Front::getInstance()->setBaseUrl('/portal/websvc/');
$application->bootstrap()
->run();
}catch(Exception $e)
{
echo 'problem'.$e->getMessage();
}
Rename
/app/modules/default/controllers/errorController.php
to
/app/modules/default/controllers/ErrorController.php
Most likely there's an exception and application is trying to display it, but is unable to load the ErrorController
I have a problem with setting up my Zend Framework application on live server. It works alright on localhost.
My live server address where I have the application is:
http://www.domainname.com/new/
Everything is OK until I try to access my admin module at URL http://www.domainname.com/new/admin , then I get the error below.
Any ideas?
An error occurred
Page not found
Exception information:
Message: Invalid controller specified (index)
Stack trace:
#0 /data/www/www.domainname.com/public_html/new/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#1 /data/www/www.domainname.com/public_html/new/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#2 /data/www/www.domainname.com/public_html/new/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#3 /data/www/www.domainname.com/public_html/new/index.php(27): Zend_Application->run()
#4 {main}
Request Parameters:
array (
'module' => 'admin',
'controller' => 'index',
'action' => 'index',
)
Include paths in index.php are set correctly (library and everything else is loaded), index.php file here:
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
set_include_path('library');
// Define upload path
if (!defined('UPLOAD_PATH'))
define('UPLOAD_PATH', realpath(dirname(__FILE__)) . '/upload/');
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
Bootstrap.php file:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype(){
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
protected function _initTimeZone(){
$date = $this->getOption('date');
date_default_timezone_set($date['timezone']);
}
protected function _initLayoutHelper(){
$this->bootstrap('frontController');
Zend_Controller_Action_HelperBroker::addHelper(
new Jakub_Controller_Action_Helper_LayoutLoader());
}
protected function _initFlashMessenger(){
$flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
if ($flashMessenger->hasMessages()) {
$view = $this->getResource('view');
$view->messages = $flashMessenger->getMessages();
}
}
protected function _initAuth(){
$this->bootstrap('session');
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$view = $this->getResource('view');
$view->user = $auth->getIdentity();
}
return $auth;
}
}
Application.ini file:
[production]
webhost = "http://www.domainname.com/new"
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
includePaths.library = APPLICATION_PATH "/../library"
date.timezone = "Europe/Bratislava"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
autoloadernamespaces.nette = "Nette_"
autoloadernamespaces.jakub = "Jakub_"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.view[] =
resources.view.helperPath.App_View_Helper = APPLICATION_PATH "/views/helpers"
resources.modules[] =
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/default/"
resources.layout.layout = default
admin.resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts/scripts/base/"
admin.resources.layout.layout = default
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
webhost = "http://domainname"
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
In your admin module folder, check that IndexController.php exists within the "controllers" sub-directory.
If it does, then open IndexController.php and ensure that the class declaration does indeed declare the class "IndexController" (a common copy+paste pitfall)
Edit: Controller name should be Admin_IndexController, not just IndexController
try in your application.ini:
resources.frontController.moduleControllerDirectoryName = "controllers"
I also have this in my application.ini for my module app:
resources.frontController.params.prefixDefaultModule = ""
also does each module have it's own bootstrap?
<?php
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {
//put your code here
}
I was using an authetication controller my collegue prepared and I had the same problem, and I found the problem in the redirect of the controller.
if (!$this->_acl->isAllowed(Zend_Registry::get('user_role'), $module . ':' . $controller, $action)) {
$request->setModuleName('default')->setControllerName('authentication')->setActionName('login');}
This basically checkes if you are logged in and if you have the access rights to go to the specified controller, and if not, it redirects you to (in this case) default/authenticaion/index
Unfortunately the redirect parameters are not displayed in the error message.
I realized I do not have the controller I am redirecting to ready, so in some cases it might be the same problem.
I have created a module 'admin' . I also created a layout for this admin module. How can I permanently attach this layout to 'admin' module. Can some one suggest me where and how can I write code for this purpose. Whether it would be in bootstrap file ?
If this is a module like you say you can simply add a layout.phtml file into the module's layout/scripts/ folder.
If you have a different name for your layout.phtml like admin.phtml you simple add the following in your controller
$this->_helper->layout->setLayout('admin');
It should and will check first the module's layout folder and then the default folder.
you can specify alternative path for layout scripts:
if (!$registered) {
//for PUBLIC role
Zend_Layout::getMvcInstance()->setLayoutPath(APPLICATION_PATH.'/layouts/scripts/pub');
} else {
//For registered users
Zend_Layout::getMvcInstance()->setLayoutPath(APPLICATION_PATH.'/layouts/scripts');
}
In your bootstrap file
protected function _initAutoloader() {
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/modules/default'), array(
'namespace' => 'Admin',
'basePath' => APPLICATION_PATH . '/modules/admin'
)
);
return $autoloader;
}
application.ini
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
includePaths.library = APPLICATION_PATH "/../library"
appnamespace = "Default"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.defaultModule = "default"
resources.frontController.defaultController = "index"
resources.frontController.defaultAction = "index"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.layout.layout = "layout"
resources.modules = ""
resources.view[] =
and in each controller of admin you need to add this line to the init function to change the layout.
$this->_helper->layout()->setLayout("admin");
I started out with models in a module called Api, using the following
classname/filename conventions:
filename: {project}/application/modules/api/models/Account.php
classname: Api_Model_Account
They were happily autoloading through the framework as expected.
I decided that some of these models would be better off,
organisationally speaking, in the application's models directory as they
could be used across multiple modules.
I moved them and renamed them:
filename: {project}/application/models/Account.php
classname: Application_Model_Account
However, they are not autoloading - PHP throws a class not found. I have checked and confirmed my
configuration ({project}/application/configs/application.ini):
[production]
appnamespace = "Application"
So, the application namespace is 'Application', but the models in the
application/models directory are not being autoloaded.
Just to make things that little bit crazier - the plugins in application/plugins are being autoloaded.
Testing:
<?php
class Application_Model_ExampleTest extends AMH_Test_PHPUnit_ControllerTestCase
{
public function testLoad()
{
$moduleModel = new Api_Model_Product();
$plugin = new Application_Plugin_ModuleErrorControllerSelector();
$applicationModel = new Application_Model_Example();
}
}
class AMH_Test_PHPUnit_ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
public function setUp()
{
$this->bootstrap = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
parent::setUp();
}
public function tearDown()
{
$this->resetRequest();
$this->resetResponse();
parent::tearDown();
}
}
Configuration:
[production]
appnamespace = "Application"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
includePaths.library = APPLICATION_PATH "/../library"
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
phpSettings.date.timezone = "Australia/Adelaide"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.params.displayExceptions = 0
resources.modules[] = ""
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
Relevant folder paths:
<project name>/
application/
models/
Example.php
modules/
api/
models/
Product.php
plugins/
ModuleErrorControllerSelector.php
Error message:
PHP Fatal error: Class 'Application_Model_Example' not found in /var/www/accounts.amh.localhost/tests/application/models/ExampleTest.php on line 9
This is using Zend Framework 1.10 (package for Ubuntu 10.04)
Not sure why this way its not loading. But another suggestion is you can set the models in include path.
set_include_path('.'
. PATH_SEPARATOR . 'application/models'
. PATH_SEPARATOR . get_include_path()
);
This method needs to be in all tests to ensure the environment is properly bootstrapped:
public function setUp()
{
$this->bootstrap = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
parent::setUp();
}
In the app-level Bootstrap, you could configure a resource loader:
protected function _initAutoloader(){
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => 'Application',
'resourceTypes' => array(
'model' => array(
'namespace' => 'Model',
'path' => 'models',
),
),
));
}
In my case it was all about one capital letter.
Model_DbTable_User -> Model_DBTable_User fixed this bug.
Model_DbTable_User was working on my local but not on the server!
My application.ini looks like this:
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules = ""
resourceses.includePaths.library = APPLICATION_PATH "/../../library"
resources.layout.layout = layout
admin.resources.layout.layout = admin
index.php looks like this
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../../library'),
APPLICATION_PATH . '/modules/admin/models',
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
My application/bootstrap.php looks like this:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initAutoload() {
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Admin_',
'basePath' => dirname(__FILE__) . '/modules/admin'
));
}
}
And finally my module Bootstrap looks like this:
<?php
class admin_Bootstrap extends Zend_Application_Module_Bootstrap {
}
I am trying to develop and administrator module. I have it set up under the folder application/modules. I get this error:
Warning: include_once(Zend\Paginator\Adapter\Select.php) [function.include-once]: failed to open stream: No such file or directory in E:\wamp\www\industrial\library\Zend\Loader.php on line 146
Warning: include_once() [function.include]: Failed opening 'Zend\Paginator\Adapter\Select.php' for inclusion (include_path='E:\wamp\www\industrial\application/../library;;E:\wamp\www\industrial\application/modules/admin/models;E:\wamp\www\industrial\library;.;C:\php5\pear') in E:\wamp\www\industrial\library\Zend\Loader.php on line 146
Fatal error: Class 'Zend_Paginator_Adapter_Select' not found in E:\wamp\www\industrial\application\modules\admin\controllers\UsersController.php on line 11
Can't understand what is wrong.
PS: i have used this tutorial to set up the module
There's no need to add include path to modules.
This is not needed at all:
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../../library'),
APPLICATION_PATH . '/modules/admin/models',
get_include_path(),
)));
But ensure that this include path contains the path to Zend Framework library.
Probably you are missing:
resources.modules[] =
in application.ini.
In addition to what takeshin said, you are NOT obeying the naming conventions....
Class names must always start with Uppercase Letter.
So change...
class admin_Bootstrap extends Zend_Application_Module_Bootstrap {}
to
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {}
Your way might work on windows but on linux it'll die...
Comment this line
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"