Thursday, 7 January 2016

Zend Framework 2 Paginator Hydrator and Basic MVC

Zend Framework 2 Paginator Hydrator and Basic MVC

Question: What is Hydrator in Zend Framework?
Hydration is a component which is used for populating an object from a set of data.


Question: Give and example of Hydrator in Zend Framework?
use \Zend\Stdlib\Hydrator;
$hydrator = new Hydrator\ArraySerializable();
$object = new ArrayObject(array());
$data = $hydrator->extract($object);



Question: What are different implements are available in Hydrator?
Zend\Stdlib\Hydrator\ArraySerializable
Zend\Stdlib\Hydrator\ClassMethods
Zend\Stdlib\Hydrator\ObjectProperty



Question: What are use of Filter in Hydrator?
The hydrator filters, allows you to manipulate the behavior. This is especially useful, if you want to extract() your objects to the userland and strip some internals.


Question: What is Zend\Paginator?
Zend\Paginator is a component for paginating collections of data and presenting that data to users.


Question: What are different adapter available for Zend\Paginator?
  1. ArrayAdapter
  2. DbSelect
  3. Iterator
  4. NullFill


Question: What are different Configuration available for Zend\Paginator?
  1. setCurrentPageNumber
  2. setItemCountPerPage
  3. setPageRange
  4. setView


Question: How to set the caching in Zend\Paginator?
$cacheObject = StorageFactory::adapterFactory('filesystem', array(
    'cache_dir' => '/tmp',
    'ttl'       => 3600,
    'plugins'   => array( 'serializer' ),
));
\Zend\Paginator\Paginator::setCache($cacheObject);



Question: What is New MVC Layer in Zend Framework2 ?
is a brand new MVC implementation focusing on performance and flexibility in ZF2.


Question: What are the components and sub-components in New MVC Layer of Zend Framework2 ?
Following are components are used.
  1. Zend\ServiceManager
  2. Zend\EventManager
  3. Zend\Http
  4. Zend\Stdlib\DispatchableInterface

Following are sub-components are used.
  1. Zend\Mvc\Router
  2. Zend\Http\PhpEnvironment
  3. Zend\Mvc\Controller
  4. Zend\Mvc\Service
  5. Zend\Mvc\View



Question: What is application structure of Zend Framework2 ?
application_root/
    config/
        application.config.php
        autoload/
            global.php
            local.php
            // etc.
    data/
    module/
    vendor/
    public/
        .htaccess
        index.php
    init_autoloader.php




Question: What is module structure of Zend Framework2 ?

module_root<named-after-module-namespace>/
    Module.php
    autoload_classmap.php
    autoload_function.php
    autoload_register.php
    config/
        module.config.php
    public/
        images/
        css/
        js/
    src/
        <module_namespace>/
            <code files="">
    test/
        phpunit.xml
        bootstrap.php
        <module_namespace>/
            <test code="" files="">
    view/
        <dir-named-after-module-namespace>/
            <dir-named-after-a-controller>/
                &lt;.phtml files&gt;</dir-named-after-a-controller></dir-named-after-module-namespace></test></module_namespace></code></module_namespace></named-after-module-namespace>



Wednesday, 6 January 2016

Zend Framework 2 Zend Email and Zend _Log


Zend Framework 2 Zend Email and Zend _Log

Question: What is Zend Log?
Zend_Log is a component used for logging in output, database, file, firebug and email etc.


Question: What are different objects in Zend Log?
Following are object which are used zend_log.
  1. A Logger: It is instance of Zend\Log\Logger is the main object for Zend Log.
  2. A Writer: It is inherits from Zend\Log\Writer\AbstractWriter and used to save the log.
  3. A Filter: It is implements Zend\Log\Filter\FilterInterface and used to NOT save the log.
  4. A Formatter: It is implements Zend\Log\Formatter\FormatterInterface and used for Formating when gives to writer.



Question: What are different priorites in Zend_log?
EMERG   = 0;  // Emergency: system is unusable
ALERT   = 1;  // Alert: action must be taken immediately
CRIT    = 2;  // Critical: critical conditions
ERR     = 3;  // Error: error conditions
WARN    = 4;  // Warning: warning conditions
NOTICE  = 5;  // Notice: normal but significant condition
INFO    = 6;  // Informational: informational messages
DEBUG   = 7;  // Debug: debug messages



Question: How to log the data? Give an simple example?
$logger = new \Zend\Log\Logger;
$writer = new \Zend\Log\Writer\Stream('php://output');
$logger->addWriter($writer);
$logger->log(\Zend\Log\Logger::INFO, 'Informational message');



Question: How to distroy the log?
$logger = null;



Question: Can use multiple writer?Give an Example?
Yes, We can do.
See Example Below:
$logger = new \Zend\Log\Logger;
$writer1 = new \Zend\Log\Writer\Stream('php://output');
$writer2 = new \Zend\Log\Writer\Stream('/path/to/second/logfile');
$logger->addWriter($writer1);
$logger->addWriter($writer2);
$logger->log(\Zend\Log\Logger::INFO, 'Informational message');



Question: Can use we use Filter?Give an Example?
$logger = new \Zend\Log\Logger;
$writer = new \Zend\Log\Writer\Stream('php://output');
$logger->addWriter($writer);

$filter = new Zend\Log\Filter\Priority(Logger::CRIT); /** Write only for critical **/
$writer->addFilter($filter);

$logger->log(\Zend\Log\Logger::INFO, 'Informational message');



Question: What are different filter available in Zend_log?
  1. Priority
  2. Regex
  3. Timestamp
  4. SuppressFilter
  5. Validator



Question: What are different Formater available in Zend_log?
  1. Simple Formater
  2. Formatting to XML
  3. Formatting to FirePhp


Question: What is Zend_Email?
Zend_Email is component which is used to send email.


Question: Give an Example of Zend_Email?
use \Zend\Mail;
$mail = new Mail\Message();
$mail->setBody('This is email content.');
$mail->setFrom('sender@example.com', 'Email From');
$mail->addTo('receiver@example.com', 'Receiver');
$mail->setSubject('This is email subject');

$transport = new \Mail\Transport\Sendmail();
$transport->send($mail);



Question: What is Zend\Mail\Transport?
Zend\Mail\Transport is interface which send an email in actual. This interface has only one method i.e send().


Question: How can we send an email using SMTP Transport?
$transport = new SmtpTransport();
$options   = new SmtpOptions(array(
    'name'              => 'localhost.localdomain',
    'host'              => '127.0.0.1',
    'connection_class'  => 'login',
    'connection_config' => array(
        'username' => 'username',
        'password' => 'password',
    ),
));
$transport->setOptions($options);

$transport->send( $mail);



Question: Can we use transport to store email in File?
Yes, We can do.