Monday 8 January 2018

Zend Framework 2 Interview questions and answers for beginners

Zend Framework 2 Interview questions and answers for beginners

Question: What is module in ZF2?
Zend Framework2 uses a module system to organised your main application-specific code within each module.
Module is separated code from main which contain its own controllers, models, forms and views, along with configuration.
File Structure of module are as following
     /module
         /Album
             /config
             /src
                 /Album
                     /Controller
                     /Form
                     /Model
             /view
                 /album
                     /album



Question: What is Service manager?
The Service Manager is an implementation of the "Service Locator" design pattern.

Keys used for the service manager should be unique across all modules, you can do
this by prepended by the module name in keys.

Service manager configuration files are merged by the module manager.
The service manager lazily instantiate services when they are needed.



Question: How to invoke a class by service manager?
$serviceManager->setInvokableClass('user_mapper', 'User\Mapper\UserMapper');



Question: How to set the routing in Zend Framework2?
In Zend framwork2, routing is set in configuration file of same module.
File path: module.config.php (Full path: \module\Album\config\module.config.php)

Example of Routing in Zend Framework
'router' => array(
    'routes' => array(
        'album' => array(
            'type' => 'segment',
            'options' => array(
                'route' => '/album[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id' => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action' => 'index',
                ),
            ),
        ),
    ),
)

Following are the example of URL with above routing.
URL 1 : /album/index (Listing)
URL 2 : /album/add (Add Album)
URL 3 : /album/edit/10 (Edit Album )
URL 4 : /album/delete (Delete Album )

If you try to access url /album/hello, it will try to search helloAction in same controller.


Question: How to connect to mysql database in Zend Framework2?
  1. open global.php(/config/autoload/global.php)
  2. Add following before the "return array" String.
        $mysqlDbName='zf2';
        $mysqlDbHost='localhost';
        $mysqlDbUsername='root';
        $mysqlDbPassword='';
    
  3. replace the db array with following
         'db' => array(
             'driver'         => 'Pdo',
             'dsn'            => "mysql:dbname={$mysqlDbName};host={$mysqlDbHost}",
             'username' => $mysqlDbUsername,
             'password' => $mysqlDbPassword,         
             'driver_options' => array(
                 PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
             ),
         ),
        




Question: How to protect from Cross Site Scripting (XSS) vulnerabilities?
You need to use a in-built function whenever you print any data to browser.
    $htmlData='Hello World! How are you?';
    echo $this->escapeHtml($htmlData);



Question: How to direct from controller in Zend Framework2?
you can use redirect method for same.
    $this->redirect()->toUrl('/album/edit/1');



Question: How to permanent Redirect in Zend Framework2?
you can use redirect method for same.
    $this->redirect()->toUrl('/album/')->setStatusCode(301);



Question: How to temporary redirect in Zend Framework2?
you can use redirect method for same.
    $this->redirect()->toUrl('/album/')->setStatusCode(302);



Question: How to Change layout from controller in Zend Framework 2.0?
Add Following code in controller.
$this->layout('layout/custom'); //custom.phtml      
File path is : module\{module}\view\layout\custom.phtml Question: How to Change layout for module level in Zend Framework 2?
  1. Open the module.config.php of the module. (path: module\{module}\config\module.config.php)
  2. Add/Update Following code in view_manager array.
            'template_map' => array(
                'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml', //Setup the layout            
                'error/404'               => __DIR__ . '/../view/error/404.phtml',    //Setup the error        
            ),