Thursday, June 27, 2013

How to use multiple layouts with different action in Zend Framework

First create alternate layout

/path/to/application/views/layouts/scripts/layout.phtml --> default layout

/path/to/application/views/layouts/scripts/admin.phtml --> admin layout

The Default layout name set in application.ini

resources.layout.layoutPath = APPLICATION_PATH “/layout/script”
resources.layout.layout = “layout”

here layout mean you have one layout.phtml file in your layout folder.

So to use different layout ,you need to create another layout file in your layout folder as admin.phtml

$this->_helper->layout->setLayout('admin');

Now you can call your alternate layout with above mention function on controller.


Full example

<?php
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */      
    }
    public function indexAction()
    {
        $this->_helper->layout->setLayout('admin');  //set alternate layout
    }
    public function userlistAction()
    {
        //default layout will be called
    }
}

Modular applications with Zend Framework 1.11

STEP 1 –

Create folder with name “modules” under your “application” folder.

STEP 2 –

Then under “modules” folder you can create folder with your module name for example suppose we are going to develop separate module for Administrator so module name should be “admin”.

Now your file structure should be something like this.




STEP 3 –

Rest of programming and folder and file structure should be same we have on our default setup of zend framework  on “application” folder.

But now your class name should be with prefix of module name.

For example – Our example module is “admin”

Then class name should be like  this–

Admin_UserController, Admin_Model_UserModel, Admin_Form_LoginForm, etc.

But file should be save with name “UserController.php” if we taking an example of controller.


STEP 4 –

We now have to make changes on “application.ini” file to make sure Zend is aware of that module
application.ini :
Append following lines.

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.defaultModule = "default"
resources.modules[] = ""



STEP 5 –

Again, the naming is important; the class name must be [module name]_Bootstrap and it must extend Zend_Application_Module_Bootstrap.Finally add a Bootstrap class to the module:

It must be stored in a file called Bootstrap.php within the root of the module.

<?php

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
       public function _initAutoload()
        {
            // Each module needs to be registered...
            $modules = array(
                'Admin',               
            );

            foreach ($modules as $module) {
                $autoloader = new Zend_Application_Module_Autoloader(array(
                    'namespace' => ucfirst($module),
                    'basePath'  => APPLICATION_PATH . '/modules/' . strtolower($module),
                ));
            }

            return $autoloader;
        }
}


The module is now ready to go.

Tuesday, June 25, 2013

Dojo Integration / configuration with Zend Framework


You can download Dojo library from its official site http://dojotoolkit.org/download/

Step 1:- 

The first and most important step is to copy external ”dojo”  into your root folder of site.

Your file structure some thing like this after copied dojo folder.

Step 2:- 

After you copy the dojo folder add the following code in your bootstrap file.

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


Means you have to add path to Dojo view helpers on your bootstrap file.

After changes your bootstrap contain following lines.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
        $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
        $viewRenderer->setView($view);
    }   
}

Step 3 :-

Next you would need to add path to dojo.js file and other module of the dojo toolkit.

So open your layout file and put following code.

 <?php
        $this->dojo()->setDjConfigOption('usePlainJson', true)
                ->addStylesheetModule('dijit.themes.tundra')
                ->setLocalPath("http://localhost/zend/dojo/dojo/dojo.js");
        echo $this->dojo();
 ?>

The only thing you need to do is to enable dojo in your template file.


Your configuration is now completed.

Now create your form as follows.

Dojo Editor example after configuration 

<?php
class Application_Form_Inquery extends Zend_Form
{
    public function init() {
        
        // Dojo-enable the form:
        Zend_Dojo::enableForm($this);         
        
        // Dojo-enable all sub forms:
        foreach ($this->getSubForms() as $subForm) {
            Zend_Dojo::enableForm($subForm);
        }
        
        //parent::__construct($options);        
        
        // Set the method for the display form to POST
        $this->setMethod('post');

        // Add an company name element
        $this->addElement('text', 'company_name', array(
            'label' => 'Company name:',
            'required' => true,
            'filters' => array('StringTrim'),
        ));

        // Add the contact_person_name element
        $this->addElement('text', 'contact_person_name', array(
            'label' => 'Contact person name:',
            'required' => true,
        ));

        // Add the phone_no element
        $this->addElement('text', 'phone_no', array(
            'label' => 'Phone no:',
            'required' => true,
        ));

        // Add the ext element
        $this->addElement('text', 'ext', array(
            'label' => 'Ext:',
            'required' => false,
        ));

        // Add the email element
        $this->addElement('text', 'email', array(
            'label' => 'Email:',
            'required' => true,
        ));

        // Add the country element
        $this->addElement('text', 'country', array(
            'label' => 'Country:',
            'required' => true,
        ));

        // Add the Description element
        /*$this->addElement('textarea', 'description', array(
            'label' => 'Description:',
            'required' => true,
        ));*/
        
        $this->addElement('editor', 'description', array(
            'label'=>'Description',
            //'extraPlugins'            => array('undo', '|', 'bold', 'italic','fontName', 'fontSize', ),
            'editActionInterval' => 2,
            'focusOnLoad'        => false,
            'height'             => '250px',
            'inheritWidth'       => true,
            //'styleSheets'        => array('../dojo/dojox/editor/plugins/resources/editorPlugins.css'),
        ));

        // Add a captcha
        $this->addElement('captcha', 'captcha', array(
            'required' => true,
            'label' => 'Captcha',
            'captcha' => array(
                'captcha' => 'Dumb',
                'wordLen' => 5,
                'timeout' => 300
            )
        ));

        // Add the submit button
        $this->addElement('submit', 'submit', array(
            'ignore' => true,
            'label' => 'Submit',
        ));

        // Add the reset button
        $this->addElement('reset', 'reset', array(
            'ignore' => true,
            'label' => 'Reset',
        ));

        // And finally add some CSRF protection
        $this->addElement('hash', 'csrf', array(
            'ignore' => true,
        ));
    }
}
?>

Friday, June 14, 2013

Remove Public from URL



You have to copy all the files from "public" into the "root" folder of site, like css, jquery or images folders and index.php (you simply put them outside) and then customize your index.php file appropriately, for example:

Old Code:-

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../zend_app/application'));



New Code:-

Modify index.php a little bit

defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));