Zend framework 2

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Mohamed IDBRAHIM

Formateur et Expert Full Stack

Animée par 

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

  • Prise en main du Zend Framework

  • Architecture MVC et composants de base

  • Base de données, Formulaire et Validation

  • Utilisation avancée des composants

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Environnement de Développement

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Installation de zend framework 2


    create-project -sdev --repository-url="https://packages.zendframework.com" 
    zendframework/skeleton-application nom_de_votre_projet 2.*
    
   cd my/project/dir
   git clone git://github.com/zendframework/ZendSkeletonApplication.git
   cd ZendSkeletonApplication
   php composer.phar self-update
   php composer.phar install

Méthode 2 : Clone repository 

Méthode 1 : Composer

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

VERSIONNER le projet sur github

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

MVC mODEL vIEW cONTROLLER

View

Controller

Router

Model

Database

Browser

User

Formation ZEND FRAMEWORK 2

elearndev.net

mOHAMED IDBRAHIM

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Structure d'un projet Zf2

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Créer un vhost

   
    <VirtualHost *:80>
       DocumentRoot "E:/xampp/htdocs/zendsofrecom/public"
       ServerName zendsofrecom.local
    </VirtualHost>

Dans le fichier :

xampp/apache/conf/extra/httpd-vhosts

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Premier programme sur ZF2

Hello World

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Créer une table

  
     CREATE TABLE product (
       id int(11) NOT NULL auto_increment,
       title varchar(100) NOT NULL,
       description varchar(255) NOT NULL,
       PRIMARY KEY (id)
     );

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Créer le Model


   namespace Product\Model;

     class Product
     {
         public $id;
         public $title;
         public $description;
    
         public function exchangeArray($data)
         {
             $this->id          = (!empty($data['id'])) ? $data['id'] : null;
             $this->title       = (!empty($data['title'])) ? $data['title'] : null;
             $this->description = (!empty($data['description'])) ? $data['description'] : null;
         }
     }

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Créer le Model TABLE

namespace Product\Model;

 use Zend\Db\TableGateway\TableGateway;

 class ProductTable
 {
     protected $tableGateway;

     public function __construct(TableGateway $tableGateway)
     {
         $this->tableGateway = $tableGateway;
     }

     public function fetchAll()
     {
         $resultSet = $this->tableGateway->select();
         return $resultSet;
     }

     public function getProduct($id)
     {
         $id  = (int) $id;
         $rowset = $this->tableGateway->select(array('id' => $id));
         $row = $rowset->current();
         if (!$row) {
             throw new \Exception("Could not find row $id");
         }
         return $row;
     }

     public function saveProduct(Product $product)
     {
         $data = array(
             'title'        => $product->title,
             'description'  => $product->description,
         );

         $id = (int) $product->id;
         if ($id == 0) {
             $this->tableGateway->insert($data);
         } else {
             if ($this->getProduct($id)) {
                 $this->tableGateway->update($data, array('id' => $id));
             } else {
                 throw new \Exception('Product id does not exist');
             }
         }
     }

     public function deleteProduct ($id)
     {
         $this->tableGateway->delete(array('id' => (int) $id));
     }
 }

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Service manager

 
    public function getServiceConfig()
         {
             return array(
                 'factories' => array(
                     'Album\Model\ProductTable' =>  function($sm) {
                         $tableGateway = $sm->get('AlbumTableGateway');
                         $table = new AlbumTable($tableGateway);
                         return $table;
                     },
                     'AlbumTableGateway' => function ($sm) {
                         $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                         $resultSetPrototype = new ResultSet();
                         $resultSetPrototype->setArrayObjectPrototype(new Album());
                         return new TableGateway('product', $dbAdapter, null, $resultSetPrototype);
                     },
                 ),
             );
         }

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Connection avec DB

    
   return array(
         'db' => array(
             'driver'         => 'Pdo',
             'dsn'            => 'mysql:dbname=stock;host=localhost',
             'driver_options' => array(
                 PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
             ),
         ),
         'service_manager' => array(
             'factories' => array(
                 'Zend\Db\Adapter\Adapter'
                         => 'Zend\Db\Adapter\AdapterServiceFactory',
             ),
         ),
     );

Fichier global.php

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Connection avec DB

return array(
     'db' => array(
         'username' => 'YOUR USERNAME HERE',
         'password' => 'YOUR PASSWORD HERE',
     ),
 );

Fichier local.php

elearndev.net

mOHAMED IDBRAHIM

Formation ZEND FRAMEWORK 2

Ajouter ce code dans le controller

// module/Product/src/Product/Controller/ProductController.php:
     public function getProductTable()
     {
         if (!$this->productTable) {
             $sm = $this->getServiceLocator();
             $this->productTable = $sm->get('Product\Model\ProductTable');
         }
         return $this->productTable;
     }
Made with Slides.com