How to add include path to custom folder in application.ini in Zend Framework?
I need access to some classes from custom folder.
Thx for answers.
Kamilos
includePaths.customfolder = APPLICATION_PATH "/../customfolder"
structure your applications:
application
data
library
public
in application.ini
data_uploads = APPLICATION_PATH "/../data/uploads"
in Bootstrap.php
public function _initDefines()
{
define('DATA_UPLOADS', $this->getOption('data_uploads'));
}
now you can use DATA_UPLOADS define in your script php! ;)
UPDATE
add this to application.ini
autoloadernamespaces[] = "Foo_"
in you libray add directory Foo! In this directory add your classes.
the filename class will be Bar.php and class name will be Foo_Bar!
Related
I have an application with three modules:default, disciplines and plans. In disciplines I have a dbtable which works fine in this module, but if I want to use the dbtable in module plans inside plans_dbtable I get
Class 'Disciplines_Model_DbTable_Disciplines' not found in C:\xampp\htdocs\proiect_mps\application\modules\plans\models\DbTable\Plans.php on line 43.
Require_once and include don't solve the problem. I have Disciplines_Boostrap and Plans_Bootstrap classes written. But it doesn't work. Any ideas?
class Plans_Model_DbTable_Plans extends Zend_Db_Table_Abstract
{
...
public function addPlan(
$year,
$name,
$code,
$domain,
$specialization,
$years)
{
// Id-ul disciplinei
$id_discipline = 0;
$discipline = new Disciplines_Model_DbTable_Disciplines();
....
}
...
}
Since you're using Zend I would not suggest your answer of having a require_once to be the best Basically if you have your configuration nice you dont need to have require_once any place. This might be of a help :
In file application.ini
;Module Configuration
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleControllerDirectoryName = "controllers"
; Enables Modules bootstrap resource plugin, so each module directory has a bootstrap.php file
resources.modules = 1
and in you BootStrap.php file
protected function _initFrontController() {
// The Zend_Front_Controller class implements the Singleton pattern
$frontController = Zend_Controller_Front::getInstance();
// look in the modules directory and automatically make modules out of all folders found
$frontController->addModuleDirectory(APPLICATION_PATH . '/modules');
// forces the front controller to forward all errors to the default error controller (may already be false by default)
$frontController->throwExceptions(false);
return $frontController;
}
And yes you will need to have Bootstrap.php for your every module
class Disciplines_Bootstrap extends Zend_Application_Module_Bootstrap
{
/**
* This file is ABSOLUTELY NECESSARY to get module autoloading to work.
* Otherwise calls to "$form = new Module_Form_MyForm()" will fail.
*/
}
I think I've resolved it myself. I had to write
require_once(APPLICATION_PATH.'/modules/disciplines/models/DbTable/Disciplines.php');
instead of
require_once '/proiect_mps/application/modules/disciplines/models/DbTable/Disciplines.php';
This also works:
require_once('/../../../disciplines/models/DbTable/Disciplines.php');
for my folder structure.
How can someone autoload every form and model for each module? Consider the following file structure:
application/
modules/
foo/
forms/
Register.php
models/
Account.php
Bootstrap.php
bar/
forms/
Publish.php
models/
Article.php
Bootstrap.php
Bootstrap.php
And for example, in foo/Bootstrap.php you have the following (non-functional) code:
class Foo_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAutoLoad()
{
$loader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH . '/modules/foo',
'namespace' => 'Foo',
));
$loader->addResourceType('form', 'forms', 'Form')
->addResourceType('model', 'models', 'Model');
return $loader;
}
}
Basic question: How can the bootstrap be modified so that it does load every form and model from the Foo module?
Extra question: Is it possible to have a global autoloader that loads in forms and models from every module? If so, how?
Edit (most common questions about the issue):
The default Zend naming conventions are being used for classes. Such as Bar_Model_Article, Bar_Model_Mapper_Article, Bar_Model_DbTable_Article, Bar_Form_Publish, ... (And are being placed in their respective folder.)
It isn't just one module that doesn't get its classes loaded, it's all of them.
There is no problem autoloading classes using the Zend autoloader when using a plain no-module application with multiple models, mappers, dbtables and forms.
Fix
As #Tim Fountain mentioned the module bootstraps weren't being run, meaning none of the automatic loading occurred that's baked into Zend. Eventually, I found where the problem was in my case. I had to remove the following lines from my configuration:
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
Agreed, the global bootstrap won't work anymore; but it's a lot better than having module bootstraps not functioning. If anyone knows how to still have the global bootstrap, feel free to leave a comment. Hope this can be of help to others with a similar problem.
The module bootstrap class sets up the module autoloader automatically, so you can remove your example _initAutoload() function leaving just an empty class and it should all just work. See: http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html#zend.loader.autoloader-resource.module
Edit: It sounds like your module bootstraps aren't being run. This is not an uncommon problem as the way it all fits together can be a bit confusing. The quickest way to verify this would be to just add an init method to one of them with an echo and an exit and see if it ever gets output.
Module bootstraps are pulled in and run by the 'modules' resource within Zend Application. You need to trigger this resource in some way because ZF won't go hunting around for module bootstraps just in case they are there. The most common way to do this is to include this line in your application.ini:
resources.modules[] = ""
alternatively you can manually setup the resource from your main Bootstrap file.
I also had it always like that. But since the release of 1.10 (wild guess), you can remove that bootstrap code and just put the following line in your application.ini:
appnamespace = "Foo"
I personally leave mine empty.
My Zend Framework setup is like this:
/application
/modules
/models
/configs
/library
/public
I want to access my models without needing prefixes or namespaces, like this:
new User() // Would refer to /application/models/User.php
I know this is a fairly simple problem, but I havn't been able to figure it out yet. I've also seen a lot of similar questions but none that I thing were this one exactly, but please forgive me if I am duplicating an existing question.
So far, I have tried changing appnamespace to "" in my config.ini with no success and adding
the following to my Bootstrap with no success:
protected function _initAutoload()
{
$autoloader = new Zend_Loader_Autoloader_Resource(array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/models',
));
}
Thanks!
You have to use the fallback autoloader..
In your Bootstrap file (or wherever you like), do this:
protected function _initAutoloader()
{
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
}
That will allow you to load any arbitrary class that is in your path, even if you've registered specific autoload prefixes in your application.ini
And to clarify, make sure you are pushing APPLICATION_PATH . "/models" into your include_path at some point..
I use Module Autoloader to autoload resources (forms, Doctrine models etc.).
I do not use Zend_Db_Table at all.
When I load any Doctrine model,
e.g. MyModule_Model_Test,
it tries to load MyModule_Model_TestTable too, so I get errors that the MyModule_Model_TestTable.php is missing.
To fix this, I may create empty class MyModule_Model_TestTable class and
everything works as expected.
But I don't need this file.
Strange that, when I move MyModule_Model_TestTable to /anyDirDeeper/MyModule_Model_TestTable without changing its name or content, the class is correctly loaded too…
How to configure Module Autoloader so it would not require this …Table classes?
I have in my application.ini:
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] =
And Module Bootstrap:
class MyModule_Bootstrap extends Zend_Application_Module_Bootstrap {}
My app structure is similar to this:
/application/
/modules/
/mymodule/
/models/
/Db/
*Mymodule_Model_Db_Test*
*Mymodule_Model_Test*
I think this issue was produced because I used the same module name and resource type name (registered by default).
Models were named: Acl_Model_Modelname and Acl_ namespace was registered with autoloader. Changed model namespace to something else and it works.
I want to use the HTMLpurifier in combination with the Zend Framework. I would love to load the Class and its files with the Zend_Loader. How would you include it? Would you just use the HTMLPurifier.auto.php or do you know a better way of doing it?
I use HTML Purifier as a filter in my Zend Framework project. Here's an altered version of my class:
require_once 'HTMLPurifier.includes.php';
require_once 'HTMLPurifier.autoload.php';
class My_Filter_HtmlPurifier implements Zend_Filter_Interface
{
protected $_htmlPurifier = null;
public function __construct($options = null)
{
// set up configuration
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.DefinitionID', 'My Filter');
$config->set('HTML.DefinitionRev', 1); // increment when configuration changes
// $config->set('Cache.DefinitionImpl', null); // comment out after finalizing the config
// Doctype
$config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
// configure caching
$cachePath = APPLICATION_PATH . '/../cache/htmlpurifier';
if (!is_dir($cachePath)) {
mkdir($cachePath, 0755, true);
}
$cachePath = realpath($cachePath);
$config->set('Cache.SerializerPath', $cachePath);
if (!is_null($options)) {
//$config = HTMLPurifier_Config::createDefault();
foreach ($options as $option) {
$config->set($option[0], $option[1], $option[2]);
}
}
$this->_htmlPurifier = new HTMLPurifier($config);
}
public function filter($value)
{
return $this->_htmlPurifier->purify($value);
}
}
Unless I'm misunderstanding the question (Or HTMLpurifier). If you have Zend_Loader running and it's set to autoload.
require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();
Or something to that effect. Put the HTMLpurifier class in your library directory. I'm just not sure on it's actual class name.
You can just put the class file in the library directory and call it by it's name, or maybe toss it in a misc package.
Examples
// SITE/library/Zend/Auth.php
class Zend_Auth
{
}
// SITE/library/htmlpurifier.php
class htmlpurifier
{
}
// SITE/library/misc/htmlpurifier.php
class Misc_HTMLpurifier
{
}
Make sense?
you can register an autoloader class using the Zend_Loader class. when you call the registerAutoLoad() method without any parameters, you are actually registering Zend_Loader itself as an autoloader. so:
Zend_Loader::registerAutoLoad();
// equals to: Zend_Loader::registerAutoLoad('Zend_Loader'),true);
Zend_Loader tries to load classes using Zend Framework's naming convention, which is like this:
each class is defined in a separate file
each class name begins with a capitalized letter
underlines in class name, means a directory level.
so if 'Zend_Loader' is the name of a class, it is defined in the file 'Loader.php' in 'Zend' directory in your path. to you PHP can file load this class from file Zend/Loader.php
if your classes follow this naming convention, they can be automatically loaded using the same autoloader. else, you need to define your own autoloader. write an autoloader class winch can extend Zend_Loader, and define the loading functionality so that it will load classes with other naming conventions. then register your own autoloader with Zend_Loader. like this:
Zend_Loader::registerAutoLoad('myLoader',true);
I've put the contents of library of the archive of HTMLPurifier in my library path. So I have this directory structure :
library/
HTMLPurifier\
HTMLPurifier.auto.php
...
HTMLPurifier.safe-includes.php
And then I put this on top of the file where I'm using the HTMLPurifier :
require_once 'HTMLPurifier.safe-includes.php';
Ugly, but it's working.