Showing posts with label Zend Framework Interview Questions and Answers. Show all posts
Showing posts with label Zend Framework Interview Questions and Answers. Show all posts

Friday, 10 October 2014

Download Videos from Amazon S3 - PHP

Download Videos from Amazon S3 - PHP

            $newFileName = "download_filename.mp4";
            header('Content-Type: video/mp4');
            header('Content-Disposition: attachment; filename="' . $newFileName . '"');
            $my_aws_key = 'AWS_KEY';
            $my_aws_secret_key = 'AWS_SCRET_KEY';
            $s3 = new Zend_Service_Amazon_S3($my_aws_key, $my_aws_secret_key);
            $newFileName = "S3_download_filename.mp4";
            $response = $s3->getObjectStream("$newFileName");//download stream from s3
            readfile($response->getStreamName());
            

How to set Cron in Zend Framework?

How to set Cron in Zend Framework?

Step 1: Don't execute bootstrap's run function directly in index.php
File Location: public_html/index.php
Update Following code:
if (php_sapi_name() != 'cli' || !empty($_SERVER['REMOTE_ADDR'])) {
 $application->bootstrap()->run();
}


Step 2: Create a folder cronjob in web application root folder.


Step 3. Create a file init.php under cronjob folder and add following code.
// Define path to application directory
defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
        || define('APPLICATION_ENV', 'environment');

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
            realpath(APPLICATION_PATH . '/../library'),
            get_include_path(),
        )));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
                APPLICATION_ENV,
                APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();



Step 4: Create a file mycron.php under cronjob folder.
Add following code in mycron.php
require_once 'init.php';
print_r($_SERVER);


Step 5. Append following code in mycron.php.
Now, you can access all models like below
$obj = new Application_Model_Logs();
In this file you can access zend's all global variable even you can access all models files with same way you had used in controller.

Step 6: Now Test the cron with following command
Login to putty and go to folder where your PHP is installed and run following code.
php C:\wamp\www\myproject\cronjob\mycron.php 




Thursday, 11 September 2014

Zend Framework Interview Questions and Answers for Experienced - Page 2

Zend Framework Interview Questions and Answers for Experienced


How to call another action from another Controller
$this->action("action","controller","module");


How to access variables in elements/header.phtml which is set in layout?
Just set the value before the render
$this->newVariable = $value;//Now you can access $this->newVariable in header.phtml
$this->render('elements/header.phtml');

How we can we get all parameter value in layout/view?

$objData = Zend_Controller_Front::getInstance()->getRequest()->getParams();


Question: How to change the viewScriptPath from controller?
$this->view->setScriptPath(APPLICATION_PATH."/views/scripts/newpath");      


How to change the Layout path from controller?
$this->_helper->layout->setLayout(APPLICATION_PATH."/layouts/scripts/newpath");


How can Zend Db be used to update multiple tables using joins?
class Application_Model_SurveyLogs extends Zend_Db_Table_Abstract {
    protected $_name = 'survey_logs';
    protected $_primary = 'id';
    function updateWithMultipleJoin(){            
        $this->getAdapter()->query('UPDATE survey_questions AS sq,survey_logs as sl SET sl.question=sq.name WHERE sq.id=sl.question_id;'); 
    }
}

Wednesday, 3 September 2014

Zend Framework Interview Questions and Answers for Experienced

Zend Framework Interview Questions and Answers for Experienced


Question: How to disable layout from controller?
$this->_helper->layout()->disableLayout();


Disable Zend Layout from controller for Ajax call only
if($this->getRequest()->isXmlHttpRequest()){
    $this->_helper->layout()->disableLayout(); 
}


How to change the View render file from controller?
function listAction(){
    //call another view file file
    $this->render("anotherViewFile");
}


How to protect your site from sql injection in zend when using select query?
$select = $this->select()                
                ->from(array('u' => 'users'), array('id', 'username'))
                ->where('name like ?',"%php%")
                ->where('user_id=?','5')
                ->where('rating<=?',10);



How to check post method in zend framework?
if($this->getRequest()->isPost()){
    //Post
}else{
//Not post
}


How to get all POST data?
$this->getRequest()->getPost();


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


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


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


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


How to create object of Model?
$userObj = new Application_Model_Users();


Which Class extend the Zend Controller?
Zend_Controller_Action
For Example
class AjaxController extends Zend_Controller_Action {
}


Which Class extend the zend Model?
Zend_Db_Table_Abstract
For Example
class Application_Model_Users extends Zend_Db_Table_Abstract { } 


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: $this->headScript();


What are Naming Convention for PHP File?
1. There should not any PHP closing tag (?>)in controller & Model file.
2. Indentation should consist of 4 spaces. Tabs are not allowed.
3. The target line length is 80 characters, The maximum length of any line of PHP code is 120 characters.
4. 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


What is Front Controller in Zend Framework?
Zend used Front Controller pattern for rendering the data. It 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.

How to use update statemnet in Zend Framework?
  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));
    }
}


How we can do multiple column ordering in Zend Framework?
  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);
    }
 
}

Tuesday, 15 July 2014

Zend Framework Interview Questions and Answers

Question: What is a framework?
In software development, a framework is a defined structure in which we can developed a new project. Its better to developed in framework than in core technology Because when we get a easy setup and many inbuilt functionalities.


Question: What are the features of framework?
Feature of Framework An abstract design Set of common functionality inbuilt Fast & Reliable Already tested by 1000 of developers We can check the reviews & rating of framework before use


Question: How to get post/get variables?
getRequest();
$request->getParams(); //All Get Variables
$request->getPost();//all Post variables
$request->getParam('id','0'); //get Id variables

Question: What is Bootstrapping?
Many PHP applications funnel server requests into a single PHP source file (only php files) that sets up the environment and configuration for the application, manages sessions, manage translate and caching, and invokes the dispatcher for their MVC framework. They can do more, but their main job is to take care of the consistent needs of every page of a web application.


Question: What is Zend Engine?
Zend Engine is used internally by PHP as a complier and runtime engine. PHP Scripts are loaded into memory and compiled into Zend opcodes. this opcodes render the html files.


Question: How Zend Routing Works?
Zend_Controller_Router_Rewrite is the standard router for zend framework. Basically Routing is the process of taking a URI endpoint and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request. This values of the module, controller, action and other parameters are packaged into a Zend_Controller_Request_Http object which is then processed by Zend_Controller_Dispatcher_Standard. Routing occurs once, when the request is initially received and before the first controller is dispatched.


Question: What are Plugins in zend framework?
Plugins are triggered by front controller events,bookend each major process of the front controller and allow automating actions that apply globally.
routeStartup – before the current route is evaluated
routeShutdown – after the completion of routing
dispatchLoopStartup – before the dispatch loop is entered
preDispatch – before the current action is dispatched
postDispatch – after the current action is dispatched
dispatchLoopShutdown – after the dispatch loop is completed


Question: How to disable Layout?
$this->_helper->layout()->disableLayout();


Question: How to disable view files?
$this->_helper->viewRenderer->setNoRender(true);


Question: How to call two different views from same action?
$this->render('differentView.phtml');


Question: How to include css from controller and view in zend framework.
From View
$this->headLink()->appendStylesheet('filename.css')
From Controller
$this->view->headLink()->appendStylesheet('filename.css');


Question: What is FrontController?
It is based on Front Controller Design 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.

Wednesday, 5 March 2014

Zend Database Query - Zend Profiler Example - Code Snippets

Zend Profiler: It is used to display the queries executed by zend indirectly with MySql. It will show all the queries like insert, update, delete etc.

Zend Profiler is very important just because it show the queries but It can help you to improve the performance of your website.

See How you can use the Zend Profiler.
  1. With zend profiler you will get to know what type of queries are running in your application and which is making slow your website.
  2. Zend Profiler also also how much time each query is taking to execute.
  3. you can get to know, what are un-necessary queries are running
  4. What queries are running multiple times
  5. For future you can store these queries for further use.


 See below Example, How to use zend Profiler

//create class
class Application_Model_Test extends Zend_Db_Table_Abstract {
    protected $_name = 'tests';
    protected $_primary = 'id';

    //create function
    function insertData($data) {
        /** enable the zend profiler **/
        $this->getAdapter()->getProfiler()->setEnabled(true);
        $profiler = $this->getAdapter()->getProfiler();
        /** enable the zend profiler **/

        //save data
        $this->insert($data);

        /** to list the mysql queries * */
        foreach ($profiler->getQueryProfiles() as $query) {
            $sqlQuery = $query->getQuery();
            $params = $query->getQueryParams();
            echo $sqlQuery = str_replace(array('?'), $params, $sqlQuery);
            echo '';            
        }
        
        /** to list the mysql queries * */
    }
}




5 Best Related Posts are Following:1. Web service - current time zone for a city- Free API
2. Zend Gdata Youtube API - Search Video - View Video Detail
3. Download the video from Amazon Simple Storage Service S3
4. How to set Cron in Zend Framework
5. Zend Cache Tutorial - Zend Framework 1.12