Showing posts with label ZF3. Show all posts
Showing posts with label ZF3. Show all posts

Thursday, 6 June 2019

How to send email from zend framework 3?

How to send email from zend framework 3?

How to send email from zend framework3?


define('MR_MAIL_HOST','smtp.sendgrid.net');
define('MR_MAIL_PORT','587');//457,25
define('MR_MAIL_USERNAME','username');  
define('MR_MAIL_PASSWORD','password');  

$transport = new Mail\Transport\Smtp();
$options   = new Mail\Transport\SmtpOptions(array(
    'name'              => 'emailName',
    'host'              => MR_MAIL_HOST,
    'connection_class'  => 'login',
    'connection_config' => array(
        'username' => (MR_MAIL_USERNAME),
        'password' => (MR_MAIL_PASSWORD),
         'port' => MR_MAIL_PORT,
    ),
));
$transport->setOptions($options);

$mail = new Mail\Message();
$mail->setFrom('from@web-technology-experts-notes.in');
$mail->setSubject('This is simple subject');
$mail->setBody('this is html body text');
$mail->addTo('to@web-technology-experts-notes.in');
$transport->send($mail);

Wednesday, 5 June 2019

Zend Framework 3 Authentication

Zend Framework 3 Authentication

use Zend\Authentication\Adapter\DbTable\CredentialTreatmentAdapter as AuthAdapter;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Storage\Session as SessionStorage;
            
$authAdapter = new AuthAdapter(
    $this->adapter,
    'admins',
    'email',
    'password',
     'md5(?)'
);

$authAdapter
    ->setIdentity($userName)
    ->setCredential($password);
$authResult = $authAdapter->authenticate();

if($authResult->isValid()){
    $result['success']=1;
    $identity=$authResult->getIdentity();

    //Save in session
    $adminDetails=$authAdapter->getResultRowObject(array('id','email'));
    $authService = new AuthenticationService();
    $authService->setStorage(new SessionStorage('AdminLogin'));
    $authService->getStorage()->write((array)$adminDetails);
}else{
    $messages = $authResult->getMessages();
    $result['msg'] =implode(' ',$messages);
}
    


HelpFul Links
https://github.com/zendframework/zend-authentication
https://github.com/zendframework/zend-authentication/blob/master/src/AuthenticationService.php

Friday, 10 May 2019

Zend 3 Interview Questions and Answers - Page 2

Zend 3 Interview Questions and Answers

Question: What is Hydrators?
Hydrators are used to create objects from different PHP array formats.
The main feature of each hydrator is a link between two separate layers, so that they are not cluttered up with unnecessary mappings, validations or filters in the classes, models etc.


Question: How to add filter in Zend3?
You can use Zend's HtmlEntities for filter the user data.
For Example:
$filter = new Zend\Filter\HtmlEntities();
print $filter->filter('<');



Question: How to set the UTC timezone for Zend?
Put the following code in public/index.php
date_default_timezone_set('UTC');



Question: What is Service Locator Pattern?
The service locator design pattern is used "Encapsulate the processes involved in obtaining a service with a strong abstraction layer".
It return the instances of services when they are requested for by the service consumers.

Following are the Design components of Service Locator Pattern.
Service Locator: It lookup services,dependencies,business object provides a simple interface to clients.
InitialContext: The InitialContext object is the start point in the lookup and creation process.
ServiceFactory: It provides the required BusinessService objects.
BusinessService: It the main object that fullfill your requirements.


Question: What is Service Manager?
Service Manager are heavely used in zend and it is based on Service Locator design Pattern.
It is used to register a service and get the server.


Question: How to install a Service Manager component?
composer require zendframework/zend-servicemanager



Question: How to register a service in Service manager?
use Zend\ServiceManager\ServiceManager; 
use Zend\ServiceManager\Factory\InvokableFactory; 
use stdClass;  
$serviceManager = new ServiceManager([ 
   'factories' => [stdClass::class => InvokableFactory::class,], 
]);



Question: How to get the object from Service Manager?
use Zend\ServiceManager\ServiceManager;  
$object = $serviceManager->get(stdClass::class);



Question: What is eventmanager?
The zend-eventmanager helps to design "High level architecture" and supports "subject/observer pattern".

It has two importants points
Event: Its event.
Listener: Listener are attached to the events and gets called when the event is triggered.
EventInterface Class: Used to specify the event itself.
EventManager class The instance of the EventManager tracks all the defined events in an application and its corresponding listeners.


Monday, 6 May 2019

Zend 3 Interview Questions and Answers

Zend 3 Interview Questions and Answers

Question: How to print MySql query in zend framework?
$select = $this->sql->select();
echo $select->from('users')
        ->columns(array('id', 'first_name', 'last_name', 'email', 'status', 'total_events' => (`10`)))
        ->where($conds)
        ->order($orderBy);



Question: What are different type of route?
Literal routes: a literal route is one that exactly matches a specific string.
Segment routes: Segment routes allow you to define routes with variable parameters.


Question: How to disabled layout from controller in Zend Framework 3?
$view= new ViewModel();
$view->setTerminal(true);
return $view;



Question: How to print Sql Query in Zend3?
echo  $select->getSqlString($this->sql->getAdapter()->getPlatform(),\Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);



Question: Give example of Inner join, Left Join and Right join?
Inner join
$select = $this->sql->select();
$select->from(array('u'=>'users'))
        ->columns(array('id','email'))
        ->join(array('eq'=>'event_qualifiers'),"eq.user_id=u.id",array('total_events','event_type'));


Left join
$select = $this->sql->select();
$select->from(array('u'=>'users'))
        ->columns(array('id','email'))
        ->join(array('eq'=>'event_qualifiers'),"eq.user_id=u.id",array('total_events','event_type'),$select::JOIN_LEFT);


Right join
$select = $this->sql->select();
$select->from(array('u'=>'users'))
        ->columns(array('id','email'))
        ->join(array('eq'=>'event_qualifiers'),"eq.user_id=u.id",array('total_events','event_type'),$select::JOIN_RIGHT);


Question: How to autoload new module?
Once you have created the file structure for new module
1) Open the composer.json file.
2. Add the following line in psr-4 under autoload.
"Newmodule\\": "module/Newmodule/src/"


like below
"autoload": {
    "psr-4": {
        "Application\\": "module/Application/src/",
        "Album\\": "module/Album/src/"
    }
}



3. Execute following command
composer dump-autoload



Question: What are two components for databases?
A) One approach is to have "model classes" like Album represent each entity in your application
and then use mapper objects that load and save entities to the database.
B) Object-Relational Mapping (ORM) technology, such as Doctrine or Propel.


Question: What is use of config/modules.config.php?
We tell the ModuleManager that what module need to load with use of modules.config.php.


Question: What is Zend engine?
Zend Engine is a set of various components, which is used internally by PHP as a compiler.


Question: What are Decorators in the Zend framework?
Zend framework utilizes the decorator pattern to render elements and forms.


Question: What are Plugins in the Zend framework?
Zend Framework makes heavy use of plugin architectures.
Plugins allow for easy extensibility and customization of the framework while keeping your code separate from Zend Framework's code.


Question: How to check post method in zend framework?
if($this->getRequest()->isPost()){
}



Question: How to get post method in zend framework?
$postData = $this->getRequest()->getPost();



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



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



Question: What is Event Manager?
It gives the ability to create event based programming. This helps to create, inject and manage new events.


Question: What is Service Manager?
It gives the ability to consume any services (PHP classes) from anywhere with a little effort.


Question: What is Module Manager?
Ability to convert a collection of PHP classes with similar functionality into a single unit called as a module. The newly created modules can be used, maintained and configured as a single unit.