Category: "Zend Framework"

Zend Framework Regex Routing Example

This ini snippet divides the URL into module, controller, and action, including an optional id. If no id is specified, a 0 (zero) is sent.

resources.router.routes.common.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.common.route = "(\w+)/(\w+)/(\w+)/?(\d+)?"
resources.router.routes.common.defaults.module = "common"
resources.router.routes.common.defaults.controller = "device"
resources.router.routes.common.defaults.action = "index"
resources.router.routes.common.defaults.id = "0"
resources.router.routes.common.map.1 = "module"
resources.router.routes.common.map.2 = "controller"
resources.router.routes.common.map.3 = "action"
resources.router.routes.common.map.4 = "id"
resources.router.routes.common.reverse = "%s/%s/%s/%d"

Zend Framework dijit.FilteringSelect Configure

To configure a FilteringSelect dijit, including client and server-side validation, with a Zend Framework .ini file, use the following syntax:

; status element
; These MUST match the definitions in the user table
elements.status.type = "FilteringSelect"
elements.status.options.label = "Status"
elements.status.options.dijitParams.searchAttr = "name"
elements.status.options.autoComplete = true
elements.status.options.required = "true"
elements.status.options.validators.inarray.validator = "InArray"
elements.status.options.validators.inarray.options.haystack[] = "pending"
elements.status.options.validators.inarray.options.haystack[] = "activated"
elements.status.options.validators.inarray.options.haystack[] = "disabled"
elements.status.options.validators.inarray.options.haystack[] = "locked"
elements.status.options.validators.inarray.options.messages.notInArray = "Invalid status"
elements.status.options.multioptions.pending = "pending"
elements.status.options.multioptions.activated = "activated"
elements.status.options.multioptions.disabled = "disabled"
elements.status.options.multioptions.locked = "locked"

Sourcing dojo from AOL's CDN under Zend Framework

This implementation uses a default layout. To set the path and name of the layout (default.phtml), the application.ini file was configured like so:

configs/application.ini

resources.layout[] =
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.layout.layout = "default"

Bootstrap.php adds dojo to thel helper path for the view in _initView. The locale is set up as well as the translation.

Bootstrap.php

protected function _initView()
    {
        Zend_Session::start();
        $session=new Zend_Session_Namespace('global');
        $options=$this->getOptions();

        $locale = new Zend_Locale(Zend_Locale::BROWSER,TRUE);
        $session->strLocale=$locale->toString();
        Zend_Registry::set('Zend_Locale', $locale);

        $language_path=$options['resources']['locale']['translation']['path'];
        $translate = new Zend_Translate(array('adapter'=>'gettext',
                'content'=>$language_path,
                'scan' => Zend_Translate::LOCALE_DIRECTORY,
                'locale'=>$locale->toString()));
        Zend_Registry::set('Zend_Translate', $translate);

        $view = new Zend_View();
        $view->doctype('XHTML1_STRICT');
        $view->headLink()->appendStylesheet('/css/base.css')
            ->appendStylesheet('/css/color.css');

        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $viewRenderer->setView($view);

        $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
        Zend_Dojo_View_Helper_Dojo::setUseDeclarative();

        $view->strLocale=$session->strLocale;

        return $view;
    }

In the controller for the form, dojo is enabled. This way, the layout only includes dojo if the form or page requires it.

        $this->view->form = $form;
        $this->view->dojo()->enable();
        if ($this->getRequest()->isPost())

In the layout, the final dojo configuration is done, if dojo is enabled.

application/layouts/scripts/default.phtml


<?php 
// application/layouts/scripts/default.phtml

echo $this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>  
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <?php echo $this->headTitle() ?>
    <?php echo $this->headMeta() ?>
    <?php echo $this->headLink() ?>
    <?php echo $this->headStyle() ?>
<?php if ($this->dojo()->isEnabled()){
        $this->dojo()
            ->setCdnBase(Zend_Dojo::CDN_BASE_AOL)
            ->setCdnVersion('1.5')
            ->setCdnDojoPath(Zend_Dojo::CDN_DOJO_PATH_AOL);
        $this->dojo()
            ->setDjConfigOption('parseOnLoad', true)
            ->setDjConfigOption('locale',$this->strLocale)
            ->setDjConfigOption('cdnbase',Zend_Dojo::CDN_BASE_AOL)
            ->setDjConfigOption('cdnversion','1.5')
            ->setDjConfigOption('cdndojopath',Zend_Dojo::CDN_DOJO_PATH_AOL)
            ->addStyleSheetModule('dijit.themes.tundra')
            ->requireModule('dijit.form.Form')
            ->requireModule('dijit.form.ValidationTextBox')
            ->requireModule('dijit.form.TextBox');
            echo $this->dojo();
   }
?>

Zend Framework - Building Forms without writing XHTML

This block of text, from the link above, describes a form input for a username element.

#
; username element
user.login.elements.username.type = "text"
user.login.elements.username.options.validators.alnum.validator = "alnum"
user.login.elements.username.options.validators.regex.validator = "regex"
user.login.elements.username.options.validators.regex.options.pattern = "/^[a-z]/i"
user.login.elements.username.options.validators.strlen.validator = "StringLength"
user.login.elements.username.options.validators.strlen.options.min = "6"
user.login.elements.username.options.validators.strlen.options.max = "20"
user.login.elements.username.options.required = true
user.login.elements.username.options.filters.lower.filter = "StringToLower"

This is the code in the Form which uses the .ini settings:

      $config = new Zend_Config_Ini($configFile, 'development');
      $form   = new Zend_Form($config->user->login);

And the entire form is displayed with:

      <?php echo $this->form ?>

With the routine, repetitive, and time-consuming exercise of writing XHTML eliminated, you can focus on the business logic, and build applications lightening fast.

You retain complete control of the output, decorators are used to apply the XHTML to the form elements. You can create one decorator for the entire system, and your whole application will have a consistent interface.

1 3