I am putting together a modular application in Zend Framework and am struggling to get module specific View Helpers to load.
My directory structure is like this...
application
---configs
---controllers
---forms
---layouts
---models
---modules
------user
---------controllers
---------forms
---------modules
---------views
------------filters
------------helpers
---------------currentUser.php
------------scripts
---------Bootstrap.php
---views
---Bootstrap.php
basically I want to access the view helper that is contained in currentUser.php but when I put
<?php echo $this->currentUser(); ?>
I get an error stating that the file cannot be found.
What do I need to add to my config file to load these helpers?
My config is something like this...
Autoloadernamespaces[] = "Zend_"
Autoloadernamespaces[] = "Default_"
Autoloadernamespaces[] = "User_"
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =
resources.modules[] =
I have worked it out...
All I need to do was to add this line to my config file:
user.resources.view.helperPath.Zend_View_Helper = APPLICATION_PATH "/modules/user/views/helpers"
Related
I have strange issue with Zend. its working fine on localhost and development machine. but on production it gives error Invalid controller specified (receipt)
Request Parameters:
array (
'controller' => 'receipt',
'action' => 'create',
'module' => 'default',
)
my application .ini is
[production]
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.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
resources.frontController.moduleControllerDirectoryName = "controllers"
resources.frontController.params.prefixDefaultModule = ""
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =
[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
any help would be appreciated
Most likely a case-sensitivity issue. Make sure the filename of the controller begins with a capital letter (ReceiptController.php) and the directory names are lowercase (modules, controllers).
try delete this line:
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
If it still doesn't work, try my configs below (for multiple modules only):
resources.modules[] = ""
resources.frontController.params.prefixDefaultModule = ""
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.moduleControllerDirectoryName = "controllers"
resources.frontController.defaultModule = "default"
resources.frontController.defaultControllerName = "index"
resources.frontController.defaultAction = "index"
resources.frontController.env = APPLICATION_ENV
I'm sorry i speak English not well
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());
}
An *.ini file has a constant: APPLICATION_PATH
When is APPLICATION_PATH set and how does it work?
See the below code for instance
; application/configs/application.ini
[production]
; PHP settings we want to initialize
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"
APPLICATION_PATH is a PHP constant used by ZendFramework to determine where you deployed/installed your project. It's usually defined in newproject/public/index.php i.e.
// 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'));
/** 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 is not a php class / file, it is a configuration file which means it follows a different syntax.
To concatenate strings and constants together, you simple put them next to each other, you don't use the dot (.) operator. One thing to watch out for is that you have to use double quotes ("), otherwise the constant will not be evaluated.
For more information, you can look into the documentation of the parse_ini() function, which is the function that's used by ZendFramework to parse configuration files.
References:
http://php.net/manual/en/function.parse-ini-file.php
http://php.net/manual/en/function.constant.php
http://framework.zend.com/manual/en/zend.application.quick-start.html
You should find its definition in public_html/index.php
Zend Frameworks uses two important dynamic constants(APPLICATION_PATH, APPLICATION_ENV) for entire framework to work correctly in what ever location it is hosted.
it has to be initialised before there are used. so there are initialised in the index.php as every request will go through the index.php in zend framework.
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