Showing posts with label ZF2. Show all posts
Showing posts with label ZF2. Show all posts

Friday 12 January 2018

Zend Framework 2 Routes

Zend Framework 2 Routes Tutorial

Question: What is routing?
Routing is the act of matching a request to a given controller.



Question: What are different type of routing?
Following are different type of routing
  1. Literal literal route is one that matches a specific string. In this one rule match to one
  2. Segment A segmented route is used for whenever your url is supposed to contain variable parameters. parameters are used to identify certain objects within your application.



Question: Give example of Literal route?
For Example:
http://domain.com/about-me
http://domain.com/contact-us
http://domain.com/terms

    'router' => array(
        'routes' => array(
        'staticpages1' => array(
             'type' => 'literal',
             'options' => array(
                 'route'    => '/about-me',
                 'defaults' => array(
                     'controller' => 'Album\Controller\Album',
                     'action'     => 'aboutme',
                 ), 
             ),
         ),            
        'staticpages2' => array(
             'type' => 'literal',
             'options' => array( 
                 'route'    => '/contactus',
                 'defaults' => array(
                     'controller' => 'Album\Controller\Album',
                     'action'     => 'contactus',
                 ),
                
             ),
         ),            
        'staticpages3' => array(
             'type' => 'literal',
             'options' => array(
                 'route'    => '/terms',
                 'defaults' => array(
                     'controller' => 'Album\Controller\Album',
                     'action'     => 'terms',
                 ),
             ),
         ), 
        ),
    ),



Question: Give example of Segment route?
For Example: http://domain.com/album
http://domain.com/album/add
http://domain.com/album/edit/1
http://domain.com/album/delete/1


    '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',
                    ),
                ),
            ),
        ),
    ),



Question: What is database abstraction layer?
A database abstraction layer is a simplified "representation of a database" in the form of a written description or a diagram.

There are 3 abstraction layers:
  1. User model
  2. Logical Model
  3. Physical Model



Question: Why we should use database abstraction?
Once after the application development with MySQL, there is requirement to change the internal database from MySQL to SQL.
IF we have developed application with database abstraction, then we need to change the interface only.
IF we have not developed application with database abstraction, then we need to update re-write the code.


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        
            ),
    




Sunday 7 January 2018

Zend Framework 2 Create modules tutorial with example

Here are example of Zend Framework 2.5 module.
To run this example you must have already installed Zend Framework 2 in system.
Download the code and put unzipped folder in "modules" folder , then run SQL queries and access with /album/index


Add New Record 
URL: http://example.com/album/add
Add New Record




Listing of Records
URL: http://example.com/album
Listing of Records




Edit the record
URL: http://example.com/album/edit/4

Edit the record




Delete the record
URL: http://example.com/album/album/delete/1

Delete the record




Question: You need following SQL Query?
CREATE TABLE album (
   id int(11) NOT NULL auto_increment,
   artist varchar(100) NOT NULL,
   title varchar(100) NOT NULL,
   PRIMARY KEY (id)
 );
 INSERT INTO album (artist, title)
     VALUES  ('The  Military  Wives',  'In  My  Dreams');
 INSERT INTO album (artist, title)
     VALUES  ('Adele',  '21');
 INSERT INTO album (artist, title)
     VALUES  ('Bruce  Springsteen',  'Wrecking Ball (Deluxe)');
 INSERT INTO album (artist, title)
     VALUES  ('Lana  Del  Rey',  'Born  To  Die');
 INSERT INTO album (artist, title)
     VALUES  ('Gotye',  'Making  Mirrors');



Click below to download the module.
Download the Module



Friday 5 January 2018

How to install Zend Framework2 in Wamp Server window7?

How to install Zend Framework2 in Wamp Server

  1. Go to home directory of wamp (For Example: c://wamp/www
  2. Download composer (If not installed)
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
    php composer-setup.php
    php -r "unlink('composer-setup.php');";
    
  3. Download the Zend Framework2
    php composer.phar create-project zendframework/skeleton-application zf2
    
    It will take few mins to download the zend framework.
  4. Copy the composer.phar to zf2 folder
    cp composer.phar zf2
    
  5. Go to zf2 directory
    cd zf2
    
  6. Virtual Host setup
    Add following code in Apache setup
    
    <virtualhost>
        DocumentRoot "D:\wamp\www\zf2\public"
        ServerName zf2.loc
        <directory public="" wamp="" www="" zf2="">
            Options FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            Allow from all
        </directory>
    </virtualhost>
    
    



Wednesday 19 April 2017

How to install Zend Framework 2 in wamp using composer?

How to install Zend Framework 2 in wamp using composer?

1. Download the composer


Go to php directory (Path:wamp\bin\php\php5.5.12) from where you can run the PHP commands.
In My Case, I goes to "D:\wamp\bin\php\php5.5.12" using cd commands.
Execute the following command.
php -r "readfile('https://getcomposer.org/installer');" | php



2. Download the Zend framework2 using composer.


You can set the path where you want to download the zf.
I have set the path to "D:/wamp/www/zf2", Please change this as per ur need.
Execute the following command.
php composer.phar create-project -sdev --repository-url="https://packages.zendframework.com" zendframework/skeleton-application "D:/wamp/www/zf2"
 


3. Virtual Host Setup


Add following code in httpd-vhosts.conf file.
My Local path: D:\wamp\bin\apache\apache2.4.9\conf\extra
<VirtualHost *:80>
    DocumentRoot "D:\wamp\www\zf2\public"
    ServerName zf2.loc
    <Directory "D:\wamp\www\zf2\public">
        Options FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>


Add following code in hosts file
My Local path is : C:\Windows\System32\drivers\etc
127.0.0.1       zf2.loc

4. ReStart wamp Server


4. Open http://zf2.loc/

It must working, if not please comment. We let u know the reason.