Category Archives: Zend Framework - Page 2

Create a logger in Zend Framework using application.ini

I am here going to show how I usually setup a logger in my Zend applications. First we add the logger settings into the application.ini:

[production]
...
;Setup logger (file)
resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/logs/basic.log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = 7
...
  • WriterName is the type of writer you want. I choose “Stream” here for logging to file
  • WriterParams is the parameters which are sent to the writer. Since i use stream i set the path to the logfile and set the “mode” to “a” (append to file)
  • FilterName is the name of the filter type. I use “Priority” so I can use loglevels
  • FilterParams is set to 7 (Debug) so that all loglevels are logged

Now we will make the logger easily accessible by putting a handle in the Zend register like this.
Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
...
    protected function _initLogger() {
        $this->bootstrap("log");
        $logger = $this->getResource("log");
        Zend_Registry::set("logger", $logger);
    }
...
}

Now, to use the logger in your application just do like this:


//Get the logger handle from the register
$logger = Zend_Registry::get('logger');

//Use the logger
$logger->log("message", 3);//Second argument is the loglevel (0-7)

Valid loglevels (straight from the Zend code:Log.php):

EMERG   = 0;  // Emergency: system is unusable
ALERT   = 1;  // Alert: action must be taken immediately
CRIT    = 2;  // Critical: critical conditions
ERR     = 3;  // Error: error conditions
WARN    = 4;  // Warning: warning conditions
NOTICE  = 5;  // Notice: normal but significant condition
INFO    = 6;  // Informational: informational messages
DEBUG   = 7;  // Debug: debug messages

Tested on Mac OSX 10.6.8 and Zend Framework 1.10.8

Zend Bootstrap “Circular resource dependency detected” error

So you have also got it :)? This is one of the more strange errors in the Zend Framework if you ask me. One way to get it is to give the init function the “wrong” name in the bootstrap class:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    
    protected function _initAutoload() {
        $loader = new Zend_Application_Module_Autoloader(array(
        'namespace' => 'Application',
        'basePath' => APPLICATION_PATH
        ));

        return $loader;
    }

    protected function _initDbAdapter() {
        $this->bootstrap('db');
        $db = $this->getResource('db');
        Zend_Registry::set('db' , $db);
    }

    protected function _initConfig() {
        Zend_Registry::set('config', $this->getOptions());
    }  
    
    protected function _initTranslate() {

        $this->bootstrap("translate");
        $translator = $this->getResource("translate");
        Zend_Registry::set('translator', $translator);
    }
 
}

You see something wrong here? I didn’t 🙂 But there is a problem with ‘initTranslate()’ function. The last part of the name is ‘Translate’ which is the same as the

$this->bootstrap("translate"); 

call. If the bootstrap call is ‘translate’ you can not name the init function ‘Translate’ (case does not matter) so you have to do this:

protected function _initTranslator() { // not '_initTranslate'

        $this->bootstrap("translate");
        $translator = $this->getResource("translate");
        Zend_Registry::set('translator', $translator);
    }

Now the name of the function and the bootstrap call are different from each other which should make the error go away

Hope this saves someone some time. Tested in Zend Framework 1.10

Disable element or option in Zend_Form_Element

When disabling an element or option in a Zend_Form we use the function setAttrib() or, if multiple options, setAttribs(). These examples are made in Zend Framework 1.10

Disable a form element in the controller

$form = new Application_Form_MyForm();
$form->getElement('elementName')->setAttrib('disable', 'disable');

This will disable the whole ‘elementName’ element.

Disable option in a radio or select element
In declaration of element:

class Application_Form_MyForm extends Zend_Form {
...
        $typeOfLap = new Zend_Form_Element_Radio('elementName');
        $typeOfLap->setRequired(true)
                ->addMultiOptions(array(
                    'A' => 'Option A',
                    'B' => 'Option B'));
...
}

In controller

$form = new Application_Form_MyForm();
$form->getElement('elementName')->setAttrib('disable', array('A'));

This will disable option ‘Option A’. If you want to also disable ‘Option B’ you can add it to the disable array: array(‘A’, ‘B’). It works the same way with select elements