Pages

Monday 23 July 2012

Zend Interview Questions and Answers

Is Zend Framework a component library or a framework?

ZF is both. Zend Framework provides all the components required for most web applications in a single distribution. But Zend Framework components are also loosely coupled, making it easy to use just a few components in a web application- even alongside other frameworks! Using this use-at-will architecture, we are implementing features commonly found in more monolithic frameworks. In fact, we are currently working on a tooling component for the 1.8 release that will make it simpler to build applications using ZF components, yet will not sacrifice the use-at-will nature of existing ZF components.

What is autoloader?
Autoloader is function that load all the object on start up.

What is use of Zend front controller?
Routing and dispatching is managed in the front controller. It collects all the request from the server and handles it.

What is the use of Bootstrap?
Apart from index if we want to do any extra configuration regarding database and other things that is done within bootstrap.

How you can set Module name, Controller name, and Action name in Zend framework?
  •  $request->setModuleName(‘front’);
  •  $request->setControllerName(‘address’);
  •  $request->setActionName(‘addresslist’); 

Configuration in Zend Framework, application.ini file?
Configuration can be done in application.ini file in Zend framework. This file in the path application/configs/application.ini.

Checking whether form posted or not in Zend framework?
$request = $this->getRequest();
$getData = $request->getParams();
$postData = $request->getPost(); 
$isPost = $request->isPost();


Fetch last inserted id, fetch all record,find and fetch a single record.
$this->_db->lastInsertId();
$this->_db->fetchAll($sql);
$this->_db->find($id);
$this->_db->fetchRow($sql);

Difference between Zend_Registry and Zend_Session?

Zend_Registry is used to store objects/values for the current request. In short, anything that you commit to Registry in index.php can be accessed from other controllers/actions (because EVERY request is first routed to the index.php bootstrapper via the .htaccess file). Config parameters and db parameters are generally prepped for global use using the Zend_Registry object.

Zend_Session actually uses PHP sessions. Data stored using Zend_Session can be accessed in different/all pages. So, if you want to create a variable named ‘UserRole’ in the /auth/login script and want it to be accessible in /auth/redirect, you would use Zend_Session.

When do we need to disable layout?
At the time of calling AJAX to fetch we need to disable layout.
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);


How to call two different views from same action?
Example1:
Public function indexAction() {
If(condition)
$this->render(‘yourview.phtml’);
Else
Index.phtml;

Example2:
Public function indexAction() {
}
Now in your index.phtml you can have this statement to call other view
$this->action(‘action name’,’controller name’,’module name’,array(‘parameter name’=>’parameter value’));

Can we call a model in view?
Yes, you can call a model in view. Simple create the object and call the method. 
$modelObj = new Application_Model_User();

Can we rename the application folder ?
yes, we can

Can we move the index.php file outside the public folder?
yes, we can

How to include js from controller and view in zend?

From within a view file: $this->headScript()->appendFile(‘filename.js’);
From within a controller: $this->view->headScript()->appendFile(‘filename.js’);
And then somewhere in your layout you need to echo out your headScript object:
<?=$this->headScript();?>


How to include css from controller and view in zend? 
From within a view file: $this->headLink()->appendStylesheet(‘filename.css’);
From within a controller: $this->view->headLink()->appendStylesheet(‘filename.css’);
And then somewhere in your layout you need to echo out your headLink object:
<?=$this->headLink();?>

How do you protect your site from sql injection in zend when using select query?

We have to quote the strings,
$this->getAdapter ()->quote ( <variable name> );
$select->where ( ” <field name> = “, <variable name> );
OR (If you are using the question mark after equal to sign)
$select->where ( ” <field name> = ? “, <variable name> );

What is zend framework?
Answer: It is opensource framework created by zend.com (PHP company). It is object oriented and follow the MVC. Zend Framework is focused on building more secure, reliable and fast development.

How to add extra HTML (i.e link) in Zend_Form?
Answer: use Desciption decorator with (escape =false in second parameter).

How can I detect if an optional file has been uploaded?
Answer:  Zend_File_Transfer_Adapter_Http's receive()  return true.

What is Zend registry?
Answer: It is container for object and values  in applications, when you set an object in zend_registry you can access in whole site.

What is Zend_Form?
Answer: It is mainly used to display the form. but along with this we use for filter, validation and formating our form. It have following elements
Element filter
Element validation
Element ordering.
Decorator
Grouping
Sub form
Rendering form elments in single call or one by one for elements.

What is zend helpers?
It can be divided in two parts,
a) Action Helper: the helper is created for controller
b) View Helper: the helper is created for view files (.phtml).


What do you know about zend layout.
Answer: It work as a site template and have following features.
                Automatic selection and rendering layout
                Provide separate scope for calling element i.e parital, render, partialLoop

What is Inflection?
Answer: It is class in zend used to modify the string like convert to lowercase, change to url by removing special chars and convert underscore to hyphen.

What is Front Controller?
Answer:  It used Front Controller pattern.  zend also use singleton pattern.
routeStartup: This function is called before Zend_Controller_Front calls on the router to evaluate the request.
routeShutdown: This function  is called after the router finishes routing the request.
dispatchLoopStartup: This is called before Zend_Controller_Front enters its dispatch loop.
preDispatch: called before an action is dispatched by the dispatcher.
postDispatch: is called after an action is dispatched by the dispatcher.

What is Zend_filter?
Zend_filter is used to filter the data as remove the tags, trailing the spaces, remove all except digits.

6 comments:

  1. How to include view helper in your application


    class Application_View_Helper_Arun extends Zend_View_Helper_Abstract
    {
    function __construct() {
    echo "File Path: \application\modules\default\views\helpers";
    }
    }

    either in application.ini
    resources.view.helperPath.Application_View_Helper = APPLICATION_PATH "/modules/default/views/helpers"

    OR in bootstrap.php

    $view->addHelperPath(APPLICATION_PATH .DIRECTORY_SEPARATOR. 'modules'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.'helpers', "Application_View_Helper");



    HOW TO CALL
    $this->arun();

    Note: "Application_View_Helper" must match helper prefex and (application.ini OR bootstrap.ini)

    ReplyDelete
    Replies
    1. In few cases, you can use
      class Zend_View_Helper_Arun extends Zend_View_Helper_Abstract
      instead of
      class Application_View_Helper_Arun extends Zend_View_Helper_Abstract

      Delete
    2. zend view helper with multiple methods?

      class Zend_View_Helper_Arun extends Zend_View_Helper_Abstract {

      function __construct() {
      echo "File Path: \application\modules\default\views\helpers";
      }
      function Arun(){
      return $this;
      }

      function fun1(){
      return 'fun1';
      }

      function fun2(){
      return 'fun1';
      }

      }

      How to call Zend View Helper
      $this->getHelper('Arun')->func1();
      $this->getHelper('Arun')->func2();

      Delete
  2. How zend works
    http://i.stack.imgur.com/UJ9B7.png

    ReplyDelete
  3. How to include Action helper in your application

    /** action helper class: Zend_Controller_Action_Helper_General **/
    /** action helper path: /application/controllers/helpers"
    class Zend_Controller_Action_Helper_General extends Zend_Controller_Action_Helper_Abstract
    {
    function direct($a)
    {
    return $a * 2;
    }

    }

    either in application.ini
    resources.frontController.actionHelperPaths.Zend_Controller_Action_Helper = APPLICATION_PATH "/controllers/helpers"

    OR in bootstrap.php
    $path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'controllers' . DIRECTORY_SEPARATOR . 'helpers';
    Zend_Controller_Action_HelperBroker::addPath($path, 'Controller_Action_Helper');

    ReplyDelete
    Replies
    1. Following are two way to call the action helper
      1. echo $this->getHelper('General')->mult2(20);
      2. echo $this->_helper->General->mult2(20);

      Delete

Please add comments only related to zend framework certification.