I am using Zend_Application_Bootstrap_Bootstrap, but framework can't load modeles.
Index.php
define('ROOT_PATH', realpath(dirname(dirname(__FILE__))));
define('APP_PATH', realpath( ROOT_PATH . '/application'));
set_include_path(realpath(ROOT_PATH . '/library') . PATH_SEPARATOR . get_include_path());
require_once 'Zend/Application.php';
$app = new Zend_Application('dev', APP_PATH . '/configs/application.ini');
$app->bootstrap()->run();
Application.ini
bootstrap = APP_PATH "/Bootstrap.php"
phpSettings.display_errors = on
phpSettings.display_startup_errors = on
phpSettings.error_reporting = E_ALL|E_STRICT
phpSettings.date.timezone = "Europe/London"
resources.frontController.controllerDirectory = APP_PATH "/controllers"
resources.frontController.throwExceptions = on
resources.view.encoding = "UTF-8"
resources.view.contentType = "text/html;charset=utf-8"
resources.view.doctype = "XHTML1_STRICT"
resources.layout.layoutPath = APP_PATH "/views/layouts"
resources.layout.layout = "layout"
resources.db.adapter = "Pdo_Mysql"
resources.db.params.host = "localhost"
resources.db.params.dbname = "foo"
resources.db.params.username = "user"
resources.db.params.password = "password"
resources.db.params.charset = "utf8"
Bootstrap.php:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
}
Folder tree:
Myproject->
/application
/configs
/controllers
/models
/Mytest.php
/views
Bootstrap.php
/public
index.php
Problem:
I trying to create new Mytest() in controller, but i get "class not found in..."
When I add to bootstrap Zend_Loader::loadClass('Mytest', APP_PATH . '/models/') everything works.
My questions is, how to setup Zend_Application_Bootstrap_Bootstrap via application.ini to autoload models folder ?
Thank you.
Model classes are by default named in ZF under 'Application' namespace.
So your class would be named as 'Application_Model_Mytest'
You can set the default namespace 'Application' by adding this to the application.ini
appnamespace = "Application"
Related
I want to get use of Controller plugins in Zend Framework.
I've created a folder "plugins" in application folder and a file Test.php inside of it. It's content:
<?php
class Plugin_Multilanguage extends Zend_Controller_Plugin_Abstract{
public function preDispatch(){
echo 'The plugin is working';
}
}
in the Bootstrap file, I have added function:
protected function _initAutoload(){
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Plugin_Multilanguage());
}
And it doesn't work. I don't get even any kind of error or worning. Just a blank page. I have noticed that there plenty of similar topics, I've read all of them, but I'm still confused with that simple issue. I use Zend Framework 1.11. Please help.
I got following error:
Fatal error: Class 'Plugin_Multilanguage' not found in C:\zendSites\moduleDeveloper\application\Bootstrap.php on line 13
My application.ini file content:
[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules = ""
resources.frontController.params.displayExceptions = 0
resources.frontController.params.prefixDefaultModule = "1"
resources.router.routes.defaultmodule.type = Zend_Controller_Router_Route_Module
resources.router.routes.defaultmodule.abstract = On
resources.router.routes.defaultmodule.defaults.module = "system"
resources.router.routes.language.type = Zend_Controller_Router_Route
resources.router.routes.language.route = ":language"
resources.router.routes.language.reqs.language = "^(fr|en)$"
resources.router.routes.language.defaults.language = "en"
resources.router.routes.default.type = Zend_Controller_Router_Route_Chain
resources.router.routes.default.chain = "language, defaultmodule"
[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
I've tried to add one by one following resources:
pluginPaths.Plugins = APPLICATION_PATH "/plugins"
autoloaderNamespaces[] = "plugins"
resources.frontController.plugins[] = "Plugin_Multilanguage"
pluginPaths.Plugins = APPLICATION_PATH "/plugins"
autoloaderNamespaces[] = "plugins"
Only the error message is changing:
Fatal error: Class 'Plugin_Multilanguage' not found in C:\zend\library\Zend\Application\Resource\Frontcontroller.php on line 117
What am I doing wrong?
The chances are that your plugin path is not readable by the application:
APPLICATION_PATH "/plugins"
These kind of paths are not readable by default for class code in ZF.
Try moving your plugins to an Application library:
/library
/Application
/Plugin
Multilanguage.php
Update your code to reflect this:
<?php
class Application_Plugin_Multilanguage extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(){
echo 'The plugin is working';
}
}
And update the bootstrap:
protected function _initAutoload()
{
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Application_Plugin_Multilanguage());
}
I'm working on a project that I'm using Zend_Hostname_Router_Route for.
My application is fairly simple. I have a a module named dev which I want to route to the dev subdomain. I have read a few articles on here and other sites to try to get a idea of what the problem is.
I am using ZF 1.12 for my application. I'm using the Zend_Hostname_Router_Route in my appllication.ini file as listed below:
[production]
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.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =
resources.view.encoding = "UTF-8"
resources.view.doctype = "HTML5"
resources.view.contentType = "text/html; charset=UTF-8"
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook.db"
[development:production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/..application/db/guestbook-dev.db"
resources.router.routes.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.route = "dev.michaeldrowan.com"
resources.router.routes.chains.dev.type = "Zend_Controller_Router_Route"
resources.router.routes.chains.dev.route = ":controller/:action/"
resources.router.routes.chains.dev.defaults.module = "dev"
resources.router.routes.chains.dev.defaults.controller = "index"
resources.router.routes.chains.dev.defaults.action = "index"
The application in the ZF quickstart tutorial. So I shouldn't have problems setting this up. I have tried two index.php. On in public_html and one in subdomain in folder below(that is where my host puts it in). The index in the subdomain points to the dev module..I also tried the way of doing it that is how the zf quickstart tutorial
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../michaeldrowan /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(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
?>
and the index in the dev is:
<?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(),
)));
/** 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();
?>
I have tried to point to the module in both by changing the 'production' to 'development'. I even tried 'development : production'
I've also tried to point the application to the module config and tried setting the application to the module too.
I'm running out of things to try. I hope someone can help me.
If you are using Apache write this in your .htaccess
RewriteCond %{HTTP_HOST} ^([a-zA-Z\-\.]+)\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ index.php?someparam=%1 [QSA]
Then you can access subdomain name in your IndexController:
if ($this->getRequest()->getParam('someparam') == 'dev') {
// use routing
}
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
I've done this before on another machine and the setup is almost identical. However, its just not working on the new machine. When I try and get Doctrine to generate my models, its putting them in the wrong place. Its creating an extra "Model" directory both for the base classes as well as the regular classes. I'm using Doctrine 1.2.3 with ZF 1.11.0. Here is my ZF application.ini:
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
phpSettings.date.timezone = "America/Denver"
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = ""
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.view[] =
autoloaderNamespaces[] = "Doctrine_"
autoloaderNamespaces[] = "App_"
doctrine.connection_string = "mysql://root#localhost/mydb"
;doctrine.cache = true
doctrine.data_fixtures_path = APPLICATION_PATH "/doctrine/data/fixtures"
doctrine.sql_path = APPLICATION_PATH "/doctrine/data/sql"
doctrine.migrations_path = APPLICATION_PATH "/doctrine/migrations"
doctrine.yaml_schema_path = APPLICATION_PATH "/doctrine/schema"
doctrine.models_path = APPLICATION_PATH "/models"
doctrine.generate_models_options.pearStyle = true
doctrine.generate_models_options.generateTableClasses = true
doctrine.generate_models_options.generateBaseClasses = true
doctrine.generate_models_options.baseClassPrefix = "Base_"
doctrine.generate_models_options.baseClassesDirectory =
doctrine.generate_models_options.classPrefixFiles = false
doctrine.generate_models_options.classPrefix = "Model_"
[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
Here is my Doctrine function from my ZF Bootstrap.php file:
protected function _initDoctrine()
{
$this->getApplication()->
getAutoloader()->
pushAutoloader(array('Doctrine_Core', 'autoload'));
$config = $this->getOption('doctrine');
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, 'Model_');
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_PEAR);
$manager->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_ALL);
$manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);
if (isset($config['cache']) && $config['cache'] == true) {
$cacheDriver = new Doctrine_Cache_Apc();
$manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
}
$connection = $manager->openConnection($config['connection_string'], 'doctrine');
$connection->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
$connection->setCharset('utf8');
return $connection;
}
And here is my doctrine.php command line script:
// 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()
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->getBootstrap()->bootstrap('doctrine');
$config = $application->getOption('doctrine');
$cli = new Doctrine_Cli($config);
$cli->run($_SERVER['argv']);
So, I'm expecting my models directory to end up like this:
models
Base
-Class1.php
-Class2.php
-Class1.php
-Class1Table.php
-Class2.php
-Class2Table.php
where the base class class names are formatted like Model_Base_Class1 and the regular class names are Model_Class1 (in the actual php files themselves). Instead, what I'm getting is:
models
Base
Model
-Class1.php
-Class2.php Model
-Class1.php
-Class1Table.php
See the extra Model directories? What is causing this? I've tried messing with the doctrine.generate_models_options.classPrefix option in my ini as well as the similar setting in my Bootstrap.php file - $manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, 'Model_');
Please help me!
Well, I'm not so smart once again. :( All it was, was a misconfiguration of my schema.yaml file. I had put Model_User as the model name instead of just User! The fixtures file does require it to be Model_User though. Problem solved.
For some frustrating reason, I configured some Modules, which seemed to work fine,
However I cannot load a Modules Models. If I move the Models to the Default they load,
but I just can't get the Framework to find them locally..
Example:
My Modules Directory is:
application\modules\books\models\books.php (books is my Model)
class Application_Module_Books_Model_Books extends Zend_Db_Table_Abstract {}
I also tried..
Books_Model_Books, Model_Books, books, Modules_.. you name it I tried it :)
My controller is in the Books Module, and is an Index Controller, and it
can never find the Local Model.
I'm using Application.ini and it is configured this way:
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
I have a BootStrap in the Modules Directory:
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
I'm on Zend Framework 1.10, and ideas.. ?
What just fixed it for me was to add the following line to application.ini
resources.modules[]=
Then i added a Bootstrap.php to the module directory ('application\modules\books\')
class Books_Bootstrap extends Zend_Application_Module_Bootstrap {
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Books_',
'basePath' => dirname(__FILE__)
));
return $autoloader;
}
}
Move the books model to 'application\modules\books\models\Books.php'
class Books_Model_Books extends Zend_Db_Table_Abstract {...}
Now you should be able to load the model in the IndexController
$model = new Books_Model_Books();
In the application.ini, add a simple line:
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
and in the method _initAutoload() inside the Bootstrap put:
$front = $this->bootstrap("frontController")->frontController;
$modules = $front->getControllerDirectory();
$default = $front->getDefaultModule();
foreach (array_keys($modules) as $module) {
if ($module === $default) {
continue;
}
$moduleloader = new Zend_Application_Module_Autoloader(array(
'namespace' => $module,
'basePath' => $front->getModuleDirectory($module)));
}
make sure the name of models inside each module are like
[name_module]_Model_[name_model]
in you case, like
class Books_Model_Books {
}
and that's all :D
The correct class name would be Books_Model_Books, but the filename of that class would need to be Books.php (note the capital 'B').
You shouldn't have a bootstrap in the modules directory, but you probably do want a bootstrap for each module directory, so you'd need a class:
class Books_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
at: application/modules/books/Bootstrap.php (again note the capital 'B').
Check the section on the module resource autloader at http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html for more info.
1-feel free to delete this cuz you don't need it any more :
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
and place this code inside you Bootstrap.php file [application bootstrap ] not the module bootstrap
public function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '' ,
'basePath' => dirname(__FILE__) . '/modules/'));
return $autoloader;
}
getting back to config you need also to add
resources.modules[] = ""
resources.frontController.defaultModule = "admin"
here is my complete config file :
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.defaultModule = "news"
resources.frontController.prefixDefaultModule = 1
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
;resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
autoloaderNamespaces[] = "xxxxxx"
resources.db.adapter = "Mysqli"
resources.db.isdefaulttableadapter = true
resources.db.params.host = "localhost"
resources.db.params.dbname = "xxxxx"
resources.db.params.username = "root"
resources.db.params.password = "root"
resources.db.params.charset = "utf8"
hopefully i didn't miss any thing