Category Archives: Zend

Zend

Difference Between Cakephp and Zend Framework

S.No
Cakephp
Zend Framewok
1
It is just framework. It is Both framework and library.
2
In View Form helper, there is form elements, validation available. In View Form helper, there is form elements, validation, filters(like strip_tags), order of elements, sub form, and decoraters are available.
3
we can render form elements one by one We can render form in one line, one by one element, element OR  decorator OR error separetly.
4
Form configuration can not done by through configuation file Zend_form can be do configuration through zend_cofig or xml
5
When cake start, It load all library that’s why Its not good for small project. Its library is too big as compare to cakephp. If you are  not using any component then it will not load by default.
6
It uses front controller design and singleton design pattern. It use front controller pattern, factory patter, strategy pattern, singleton pattern.
7
It is good for fast development. It is best, if you application are using webservices
8
ACL, Auth like comonents are easily availbele for cakephp. It has its own ACL, Auth components and upto deeper level.
9
When cake start, It load all library that’s why Its not good for small project. Its library is too big as compare to cakephp. If you are  not using any component then it will not load by default.
10
It have session component It have Zend Session and Zend Registery components.
11
Its class name does not map with file location Its mapping,
If any class name is Zend_forms_abc
file location is Zend/Forms/abc.php
12
Mostly used for fast development. Mostly used for web services

How to set multiple layout in Zend FrameWork

During developing an application we need to set the multiple layout for a particular controller, action, view.

Set layout for a particular controller or action:

Within Controller/action

1
2
3
4
5
<?php   
(a) Zend_Layout::getMvcInstance()->setLayout('layout_name');
            
(b) $this->_helper->layout()->setLayout('layout-user');
?>

Within View

1
<?php $this->layout()->setLayout('layout_name'); ?>

Disable layout for a particular controller or action:

Within controller, action

1
2
3
4
<?php
$this->_helper->layout()->disableLayout(); 
$this->_helper->viewRenderer->setNoRender(true);
?>

How to set Layout using Plugin in Zend FrameWork

Some time we need to set different layout based on user type. So the best way to handle it by using plugin.

Bootstrap.php:

1
2
3
4
5
6
7
8
9
10
11
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public function _initPlugins()
    {
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new Zend_Controller_Plugin_Acl()); // Plugin file path library/zend/controller/Plugin/Acl.php
    }
}
?>

In library/zend/controller/Plugin/Acl.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
class Zend_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch()
    {
        $user = Zend_Auth::getInstance();
        $role = $user->getIdentity()->role;
        $layout = Zend_Layout::getMvcInstance();

        switch ($role) {
            case 'admin':
                $layout->setLayout('layout1');
                break;

            case 'user':
                $layout->setLayout('layout2');
                break;

            default:
                $layout->setLayout('layout');
                break;
        }
    }
}
?>

Hope! you will enjoy.

How to create/setup custom routing in ZendFramework

Routing is the process of taking a URI endpoint (that part of the URI which comes after the base URL) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request.
In simple way, we can say that this is same as URL Rewriting in PHP we do.

Ex1 : Routing without variables

Now, say for example your domain is http://mydomain.com
And you want the http://mydomain.com/register/user to be http://mydomain.com/user-registration

Put the code below in Bootstrap.php file as

1
2
3
4
5
6
7
<?php
protected function _initRoute()
{
    $router = Zend_Controller_Front::getInstance()->getRouter(); //create object of a router
    $router->addRoute('registration',new Zend_Controller_Router_Route('user-registration',array('controller'=>'register','action'=>'user'))); //registration can be any text as you want
}
?>

Ex2 : Routing with variables

Now, say for example http://mydomain.com/product/10

Put the code below in Bootstrap.php file as

1
2
3
4
5
6
7
<?php
protected function _initRoute()
{
    $router = Zend_Controller_Front::getInstance()->getRouter(); //create object of a router
    $router->addRoute('myproduct',new Zend_Controller_Router_Route('product/:id',array('controller'=>'product','action'=>'index'))); //myproduct can be any text as you want 
}
?>

Hope, you will enjoy!