I created a custom XML configuration file called config.xml and placed it in the configs directory in the Zend Framework. I want to use it in one of my controllers using Zend_Config_Xml. What I have is not working, and it says "An error occurred. Application error". How do I read in a custom XML config file from a controller? This is what I have in my controller so far:
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
$config = new Zend_Config_Xml('config.xml', 'staging');
echo $config->host;
}
}
Probably just the path you need to fix:
$config = new Zend_Config_Xml(APPLICATION_PATH.'/configs/config.xml', 'staging');
if not, check the error log to see what the actual error message is.
Edit: To do this in the bootstrap, the easiest (although perhaps not best) way is to add a new resource method and store the config object in the registry. Add this to your bootstrap class:
protected function _initCustomConfig()
{
$config = new Zend_Config_Xml('config.xml', 'staging');
Zend_Registry::set('config', $config);
return $config;
}
you can then access it later using:
$config = Zend_Registry::get('config');
If you're debugging the issue locally, first enable better error reporting by adding these commands in your application.ini's development section:
phpSettings.error_reporting = E_ALL
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
By default, zend framework doesn't show internal errors.
If you're loading a Zend_Config file, it's always better to load it using an absolute path.
public function indexAction()
{
// action body
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/config.xml', 'staging');
echo $config->host;
}
Related
im following some example for doing login in Zend
using Zend_Form
but i get an error when trying to load a view
Fatal error: Class 'Application_Form_Login' not found in
/Users/manuel/projects/zend_template/zend_template/application/controllers/AuthController.php
on line 13
this is the code on that file on that line
public function indexAction()
{
[line 13] $form = new Application_Form_Login();
...}
i Have a form on
application\forms\Login.php
created on CLI
and this one have the form code
class Application_Form_Login extends Zend_Form
{
public function init()
{ ...
So, i have my Application_Form_Login
but what is the problem?
thanks
try checking your application.ini file to see if it has the 'Application' namespace registered. If your models/forms etc begin with Application_ you have to configure the autoloader to find them.
In application.ini add:
appnamespace = "Application"
Or in your bootstrap class add this:
protected $_appNamespace = 'Application';
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.
I have this custom error handler:
`class AppError extends ErrorHandler {
function error404($params) {
$this->controller->layout = 'public';
$this->controller->set('title','Droptor Page Not Found');
parent::error404($params);
}
}`
And I can't seem to use any layout that has this:
$javascript->link('jquery',true)
So the JS helper isn't loaded. But if I include this in the controller: var $helpers = array('javascript'); it still doesn't work. Nor does App::import('Helper', 'javascript');
Crap, I didn't read your question.
To add a helper to your error controller, just add this line:
$this->controller->helpers = array('Javascript');
There are two ways to do it:
First, you can create an app_controller to include every component and helper that you need on all your controllers.
Second, you can load the specific resources needed to your error controller. Create a file named error.php in your app's root (NOT webroot) with the following code:
<?php
class AppError extends ErrorHandler {
function error404($params) {
$this->controller->helpers = array('Javascript');
parent::error404($params);
}
}
You can also set a custom title with
$this->controller->set('title_for_layout', "We couldn't find what you are loooking for");
Good luck.
First off, you don't need to create your own handler if all you're doing is handling a common error type, such as 404. Custom error handlers are for your application specific errors.
If you want to simply change the layout of your page when you get a 404 error, this has been answered over here.
function beforeRender() {
if($this->name == 'CakeError') {
$this->layout = false;
}
}
And you can cause it using the line:
$this->cakeError('error404');
I am starting to write some test cases for controller classes using Zend Framework 1.10.6 and Zend_Test_PHPUnit_ControllerTestCase. I am having problems with one item, which is that while the test cases are running, Zend_Controller_Action_HelperBroker can't find the Layout action helper.
Here are the bare bones of my test case:
require_once 'PHPUnit/Framework.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
require_once 'controllers/IndexController.php';
class Application_Controllers_IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase {
public $_application;
protected function setUp() {
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp ();
}
public function appBootstrap() {
// Create application, bootstrap, but don't run
$this->_application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$this->_application->bootstrap();
$this->getFrontController()->setParams($this->_application->getOptions())
->addControllerDirectory(APPLICATION_PATH . '/controllers');
}
public function testIndexAction() {
$this->dispatch('/index/index');
$this->assertController('index');
$this->assertAction('index');
}
}
I get an exception when I run the test case:
Zend_Controller_Action_Exception: Action Helper by name Layout not found
When I comment out the two lines in class Zend_Controller_Action_HelperBroker to try to find the source of this around line 368, I get:
Zend_Loader_PluginLoader_Exception: Plugin by name 'Layout' was not found in the registry; used paths:
Zend_Controller_Action_Helper_: Zend/Controller/Action/Helper/
The loading of layout scripts works fine in my application when running, it appears that the correct path or registry for the Zend_Controller_Action_Helper can't be found while running tests under PHPUnit and therefore the Layout plugin can't be loaded.
I have verified that Zend is installed correctly and that Layout.php is in the correct place.
Any ideas?
Del
In you appBootstrap() at the end place this line:
Zend_Controller_Action_HelperBroker::addHelper(new Zend_Layout_Controller_Action_Helper_Layout);
At which point do you add your Layout code?
Remember that the 'boostraping' is different when running a PHPUnit test, and that things that are bootstrapped in your main app might not be when running as a PHPUnit test.
My workaround:
function someAction() {
// workaround for unit tests 'Action Helper by name Layout not found'
if ($this->_helper->hasHelper('layout')) {
$this->_helper->layout->disableLayout(); // disable layouts
}
...
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.