Tuesday, May 10, 2016

Top Zend Framework Interview Questions with Answer

Zend is most popular PHP Framework by function and features that makes Zend unique from other frameworks, thousand of components and library and more. it's  a MVC based architecture that makes web application development makes popular and efficient.
If you go to any interview for Zend developer position, there are lot of questions and answers which can help you to crack the interview.



What is a framework?
In software development, a framework is a defined support structure in which another software project can be organized and developed.

Why should we use framework?
Framework is a structured system where we get following things
Rapid application development(RAD).
Source codes become more manageable.
Easy to extend features.

Where we set configuration in zend framework?
We set the config in application.ini which is located in application/configs/application.ini.

What is Front Controller?
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 full form of CLA in Zend Framework?
Contributor License Agreement

Should I sign an individual CLA or a corporate CLA?
If you are contributing code as an individual- and not as part of your job at a company- you should sign the individual CLA. If you are contributing code as part of your responsibilities as an employee at a company, you should submit a corporate CLA with the names of all co-workers that you foresee contributing to the project.

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:

headScript();?>


What are Naming Convention for PHP File?

  • There should not any PHP closing tag (?>)in controller & Model file.
  • Indentation should consist of 4 spaces. Tabs are not allowed.
  • The target line length is 80 characters, The maximum length of any line of PHP code is 120 characters.
  • Line termination follows the Unix text file convention. Lines must end with a single linefeed (LF) character. Linefeed characters are represented as ordinal 10, or hexadecimal 0x0A
What are Naming Convention for Classes, Interfaces, FileNames, Functions, Methods, Variables and constants?
http://framework.zend.com/manual/1.11/en/coding-standard.naming-conventions.html 


What are coding style of Zend Framework?
http://framework.zend.com/manual/1.12/en/coding-standard.coding-style.html

How to use update statemnet in Zend Framework?
[code]
  class Application_Model_Users extends Zend_Db_Table_Abstract {
    protected $_name = 'users';
    protected $_primary = 'id';

    function updateData($updateData = array()) {
        //please update dynamic data
        $this->update(array('name' => 'arun', 'type' => 'user'), array('id=?' => 10));
    }
}
[/code]

How we can do multiple column ordering in Zend Framework?
[code]
  class Application_Model_Users extends Zend_Db_Table_Abstract {
    protected $_name = 'users';
    protected $_primary = 'id';

    function users() {
        $select = $this->select()
        ->setIntegrityCheck(false)
        ->from(array('u' => 'users'), array('name as t_name'))->order('first_name asc')->order('last_name des');
         return $this->fetchAll($select);
    }

}
[/code]

How to get all GET data?
[code]
$this->getRequest()->getParams();
[/code]

How to redirect to another page from controller?
[code]
$this->_redirect('/users/login');
[/code]

How to get variable's value from get?
[code]
$id= $this->getRequest()->getParam('id');
[/code]

Create Model file in zend framework?
[code]
class Application_Model_Users extends Zend_Db_Table_Abstract {
    protected $_name = "users";
    protected $_primary = "id";    
}
[/code]

How to create object of Model?
[code]
$userObj = new Application_Model_Users();
[/code]

Which Class extend the Zend Controller?

Zend_Controller_Action
For Example
[code]
class AjaxController extends Zend_Controller_Action {
}
[/code]

Which Class extend the zend Model?
Zend_Db_Table_Abstract
For Example
[code]
class Application_Model_Users extends Zend_Db_Table_Abstract { }
[/code]

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 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: 
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 (  );
$select->where ( "  = ",  );
OR (If you are using the question mark after equal to sign)
$select->where ( " = ? ",  );

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 is the difference between Zend_Auth and Zend_Acl?
Zend_Auth is used for authenticating users with a variety of authentication methods, including LDAP, OpenID, and HTTP. Authentication is the process of verifying that the provided credentials are valid for the system. By authenticating to your system, your users can prove that they are who they say they are. For more information on Zend Framework's authentication implementation, see the Zend_Auth documentation.
Zend_Acl is an implementation of Access Control List (ACL) authorization. Generally speaking, ACLs are lists of roles that are authorized to perform a particular operation on specific resources in your system. Zend_Acl can support advanced rule definitions with features such as multiple inheritance for roles and assertions for conditional rules. For more information on Zend_Acl, see the Zend_Acl documentation.
Zend_Auth and Zend_Acl can be used together to build very sophisticated security systems: first the user confirms their identity with Zend_Auth, then this identity is used to assign one or more Zend_Acl roles to the user for authorization to use or modify resources in the system



1 comment: