Friday, 12 January 2018

Zend Framework 2 Routes Tutorial

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.


Wednesday, 10 January 2018

PHP namespace example - Tutorial

PHP namespace example - Tutorial

Question: What is namespace in PHP?
namespace is reserve keywords which is used to encapsulating the class, method, data member variables and static methods.


Question: Benefits of namespace in PHP?
  1. Encapsulate the data which fixed collisions between code you create, and internal PHP classes/functions/constants.
  2. Ability to alias which improved the readability of source code.



Question: From which version php support namespaces?
PHP5.3



Question: How namespaces are defined?
namespaces are defined with keyword namespace
For Example:
namespace math



Question: Example - Simple class without namespace (OR Global namespace)?
File: /myproject/simple-file.php
class mathSimple {

    function add($a, $b) {
        return $a + $b;
    }

}

File: /myproject/run-example.php
include "simple-file.php";

$obj1 = new mathSimple(); //calling for Global Namespace
echo $obj1->add(10,20);//30

When we execute run-example.php file, it will return 30


Question: Example - Simple class with namespace?
File: /myproject/namespace-file.php
namespace math;

class mathSimple {

    function add() {        
        return array_sum(func_get_args()) ;
    }

}

File: /myproject/run-example.php
namespace math;
include "simple-file.php";
include "namespace-file.php";


$obj1 = new \mathSimple(); //calling for Global Namespace
echo $obj1->add(10,20,30);

echo "\n";
$obj2 = new mathSimple(); //calling for namespace math
echo $obj2->add(10,20,30);
Question: How to import namespaces?
use Zend\View\Model\ViewModel;



Question: How to use alias namespaces for import namespace?
use Zend\View\Model\ViewModel as ZendViewModel;



Question: Can we override the php in-built using namespace? Yes, We can do. File: /myproject/namespace-file2.php
namespace math;

class mathSimple {

    function add() {        
        return array_sum(func_get_args()) ;
    }

}
function strlen($string){
    return 'in built functi';
}


File: /myproject/run-example2.php
namespace math;
include "namespace-file2.php";

echo strlen('string'); //calling namespace function
echo \strlen('strlen'); //calling inbuilt function