SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
The 'M' in MVC
Zend Framework
About
Twitter: @eddiejaoude

GitHub: eddiejaoude (http://eddiejaoude.github.com)

Email: eddie@jaoudestudios.com
MVC
MVC
●   Model - This is the part of your application that defines its basic
    functionality behind a set of abstractions. Data access routines and some
    business logic can be defined in the model.

●   View - Views define exactly what is presented to the user. Usually
    controllers pass data to each view to render in some format. Views will
    often collect data from the user, as well. This is where you're likely to find
    HTML markup in your MVC applications.

●   Controller - Controllers bind the whole pattern together. They manipulate
    models, decide which view to display based on the user's request and
    other factors, pass along the data that each view will need, or hand off
    control to another controller entirely. Most MVC experts recommend »
    keeping controllers as skinny as possible.
Model
● Entity   (business object)




● DAO   (data access object)




● Mapper        (mapping from external to internal)




● Service      (NOT web service, business logic)
Interface
Object interfaces allow you to create code which specifies which
methods a class must implement, without having to define how these
methods are handled.
Interfaces are defined using the interface keyword, in the same way
as a standard class, but without any of the methods having their
contents defined.
All methods declared in an interface must be public, this is the nature
of an interface.
Interface
// Declare the interface 'iTemplate'
interface iTemplate
{
    public function setVariable($name, $var);
    public function getHtml($template);
}



// Implement the interface
class Template implements iTemplate
{
    private $_vars = array();

    public function setVariable($name, $var)
    {
        $this->_vars[$name] = $var;
        return $this;
    }

    public function getHtml($template)
    {
        foreach($this->_vars as $name => $value) {
            $template = str_replace('{' . $name . '}', $value, $template);
        }

        return $template;
    }
}
Interface
// Declare the interface 'iTemplate'
interface iTemplate
{
    /**
      * Set variable
      *
      * @param string $name
      * @param int $var
      *
      * @return iTemplate
      */
    public function setVariable($name, $var);

    /**
      * Get html
      *
      * @param string $template
      *
      * @return string $template
      */
    public function getHtml($template);
}
Entity
class ModelEntitiesUser
{
     /**
      * @var string
      */
    private $_name = '';

    /**
      * Set variable
      *
      * @param string $name
      * @return ModelEntitiesUser
      */
    public function setName($name)
    {
       $this->_name = (string) $name;
        return $this;
    }

    /**
      * Get name
      *
      * @return string
      */
    public function getName()
    {
        return $this->_name;
    }

}
DAO Interface
interface ModelDaosUserInterface
{
    /**
      * Find all
      *
      * @return stdClass
      */
    public function findAll();

    /**
      * Find by ID
      *
      * @param int $id
      *
      * @return stdClass
      */
    public function findById($id);
}
DAO Database
//$_datasource = new ZendDb;
class ModelDaosUserDb implements ModelDaosUserInterface
{
    /**
      * Find all
      *
      * @return stdClass
      */
    public function findAll()
    {
         return $this->getDatasource()->findAll();
    }

    /**
      * Find by ID
      *
      * @param int $id
      *
      * @return stdClass
      */
    public function findById($id)
    {
         return $this->getDatasource()->findById($id);
    }
}
DAO Soap Client
//$_datasource = new ZendSoapClient;
class ModelDaosUserSoap implements ModelDaosUserInterface
{
    /**
      * Find all
      *
      * @return stdClass
      */
    public function findAll()
    {
        $data = $this>getDatasource()->findAll();
         return $data;
    }

    /**
      * Find by ID
      *
      * @param int $id
      *
      * @return stdClass
      */
    public function findById($id)
    {
        $data = $this->getDatasource()->findById(array('id' => 1));
         return $data;
    }
}
Mapper
//$_dao = new ModelDaosUserSoap;
//$_dao = new ModelDaosUserDb;
class ModelMappersUserDb
{
    /**
      * Find all
      *
      * @return array
      */
    public function findAll()
    {
        $users = array();
        foreach($this->getDao()->findAll() as $user) {
            $userEntity = new ModelEntitiesUser;
            $userEntity->setName($user->name);
            $users[] = $userEntity;
        }

        return $users;
    }

}
Service
//$_mapper = new ModelMappersUser;
class ModelServicesUser
{
    /**
      * Find all
      *
      * @return array
      */
    public function findAll()
    {
         // business logic
         // ..

        return $this->getMapper()->findAll()
                                           ;
    }

}
Verbose Usage
$dao = new ModelDaosUserSoap;
$mapper = new ModelMappersUser($dao);
$service = new ModelServicesUser($mapper);

$users =$service->findAll();




                             DIC
                 Dependency Injection Container
Service / Mapper
Single Service Multiple Mappers



      Service             Mapper                   DAO
      (User)              (User)                  (Soap)



                findAll            findEnabled


                                   findDisabled
Service / Mapper
Multiple Services Single Mapper



      Service                  Mapper              DAO
      (User)                   (User)             (Soap)



                findEnabled             findAll


                findDisabled
Model Overview
    Model                                              Datasource



                                                          DB
                                           Interface

                  Entity / Collection



            Service              Mapper     DAO           API




                                  Entity                  FILE
Next
       Model                                               Datasource



                                   Interface                  DB
                                               Interface

                     Entity / Collection



               Service              Mapper      DAO           API




                                     Entity                   FILE

Contenu connexe

Tendances

Zend Framework and the Doctrine2 MongoDB ODM (ZF1)
Zend Framework and the Doctrine2 MongoDB ODM (ZF1)Zend Framework and the Doctrine2 MongoDB ODM (ZF1)
Zend Framework and the Doctrine2 MongoDB ODM (ZF1)Ryan Mauger
 
ActionScript3 collection query API proposal
ActionScript3 collection query API proposalActionScript3 collection query API proposal
ActionScript3 collection query API proposalSlavisa Pokimica
 
What's New in Drupal 8: Entity Field API
What's New in Drupal 8: Entity Field APIWhat's New in Drupal 8: Entity Field API
What's New in Drupal 8: Entity Field APIDrupalize.Me
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate TutorialRam132
 
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...ZFConf Conference
 
A resource oriented framework using the DI/AOP/REST triangle
A resource oriented framework using the DI/AOP/REST triangleA resource oriented framework using the DI/AOP/REST triangle
A resource oriented framework using the DI/AOP/REST triangleAkihito Koriyama
 
MVC on the Server and on the Client: How to Integrate Spring MVC and Backbone...
MVC on the Server and on the Client: How to Integrate Spring MVC and Backbone...MVC on the Server and on the Client: How to Integrate Spring MVC and Backbone...
MVC on the Server and on the Client: How to Integrate Spring MVC and Backbone...jaxconf
 
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and SimpleDrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and SimpleAlexander Varwijk
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindiappsdevelopment
 
Drupal Field API. Practical usage
Drupal Field API. Practical usageDrupal Field API. Practical usage
Drupal Field API. Practical usagePavel Makhrinsky
 
Coding for Scale and Sanity
Coding for Scale and SanityCoding for Scale and Sanity
Coding for Scale and SanityJimKellerES
 
Funcitonal Swift Conference: The Functional Way
Funcitonal Swift Conference: The Functional WayFuncitonal Swift Conference: The Functional Way
Funcitonal Swift Conference: The Functional WayNatasha Murashev
 
Introduction to DI(C)
Introduction to DI(C)Introduction to DI(C)
Introduction to DI(C)Radek Benkel
 
jQuery - Chapter 4 - DOM Handling
jQuery - Chapter 4 - DOM Handling jQuery - Chapter 4 - DOM Handling
jQuery - Chapter 4 - DOM Handling WebStackAcademy
 
Building a Pluggable Plugin
Building a Pluggable PluginBuilding a Pluggable Plugin
Building a Pluggable PluginBrandon Dove
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful softwareJorn Oomen
 

Tendances (20)

Zend Framework and the Doctrine2 MongoDB ODM (ZF1)
Zend Framework and the Doctrine2 MongoDB ODM (ZF1)Zend Framework and the Doctrine2 MongoDB ODM (ZF1)
Zend Framework and the Doctrine2 MongoDB ODM (ZF1)
 
ActionScript3 collection query API proposal
ActionScript3 collection query API proposalActionScript3 collection query API proposal
ActionScript3 collection query API proposal
 
What's New in Drupal 8: Entity Field API
What's New in Drupal 8: Entity Field APIWhat's New in Drupal 8: Entity Field API
What's New in Drupal 8: Entity Field API
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
 
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
 
A resource oriented framework using the DI/AOP/REST triangle
A resource oriented framework using the DI/AOP/REST triangleA resource oriented framework using the DI/AOP/REST triangle
A resource oriented framework using the DI/AOP/REST triangle
 
MVC on the Server and on the Client: How to Integrate Spring MVC and Backbone...
MVC on the Server and on the Client: How to Integrate Spring MVC and Backbone...MVC on the Server and on the Client: How to Integrate Spring MVC and Backbone...
MVC on the Server and on the Client: How to Integrate Spring MVC and Backbone...
 
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and SimpleDrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
DrupalJam 2018 - Maintaining a Drupal Module: Keep It Small and Simple
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 
Drupal 8 migrate!
Drupal 8 migrate!Drupal 8 migrate!
Drupal 8 migrate!
 
Solid principles
Solid principlesSolid principles
Solid principles
 
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA
 
Drupal Field API. Practical usage
Drupal Field API. Practical usageDrupal Field API. Practical usage
Drupal Field API. Practical usage
 
Coding for Scale and Sanity
Coding for Scale and SanityCoding for Scale and Sanity
Coding for Scale and Sanity
 
Funcitonal Swift Conference: The Functional Way
Funcitonal Swift Conference: The Functional WayFuncitonal Swift Conference: The Functional Way
Funcitonal Swift Conference: The Functional Way
 
Introduction to DI(C)
Introduction to DI(C)Introduction to DI(C)
Introduction to DI(C)
 
jQuery - Chapter 4 - DOM Handling
jQuery - Chapter 4 - DOM Handling jQuery - Chapter 4 - DOM Handling
jQuery - Chapter 4 - DOM Handling
 
Dci in PHP
Dci in PHPDci in PHP
Dci in PHP
 
Building a Pluggable Plugin
Building a Pluggable PluginBuilding a Pluggable Plugin
Building a Pluggable Plugin
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful software
 

Similaire à Models Best Practices (ZF MVC)

How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony AppsKris Wallsmith
 
Laravel - Speaking eloquent eloquently
Laravel - Speaking eloquent eloquentlyLaravel - Speaking eloquent eloquently
Laravel - Speaking eloquent eloquentlyLaravel Nigeria
 
ZF2 for the ZF1 Developer
ZF2 for the ZF1 DeveloperZF2 for the ZF1 Developer
ZF2 for the ZF1 DeveloperGary Hockin
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolGordon Forsythe
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your CodeDrupalDay
 
Drupaljam xl 2019 presentation multilingualism makes better programmers
Drupaljam xl 2019 presentation   multilingualism makes better programmersDrupaljam xl 2019 presentation   multilingualism makes better programmers
Drupaljam xl 2019 presentation multilingualism makes better programmersAlexander Varwijk
 
PHP: GraphQL consistency through code generation
PHP: GraphQL consistency through code generationPHP: GraphQL consistency through code generation
PHP: GraphQL consistency through code generationAlexander Obukhov
 
Modularity and Layered Data Model
Modularity and Layered Data ModelModularity and Layered Data Model
Modularity and Layered Data ModelAttila Jenei
 
MVC on the server and on the client
MVC on the server and on the clientMVC on the server and on the client
MVC on the server and on the clientSebastiano Armeli
 
Zend Framework 2 - Basic Components
Zend Framework 2  - Basic ComponentsZend Framework 2  - Basic Components
Zend Framework 2 - Basic ComponentsMateusz Tymek
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
Drupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency InjectionDrupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency InjectionPhilip Norton
 
Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019julien pauli
 

Similaire à Models Best Practices (ZF MVC) (20)

Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
Doctrine and NoSQL
Doctrine and NoSQLDoctrine and NoSQL
Doctrine and NoSQL
 
Laravel - Speaking eloquent eloquently
Laravel - Speaking eloquent eloquentlyLaravel - Speaking eloquent eloquently
Laravel - Speaking eloquent eloquently
 
Speaking Eloquent Eloquently
Speaking Eloquent EloquentlySpeaking Eloquent Eloquently
Speaking Eloquent Eloquently
 
ZF2 for the ZF1 Developer
ZF2 for the ZF1 DeveloperZF2 for the ZF1 Developer
ZF2 for the ZF1 Developer
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
Doctrine for NoSQL
Doctrine for NoSQLDoctrine for NoSQL
Doctrine for NoSQL
 
Drupaljam xl 2019 presentation multilingualism makes better programmers
Drupaljam xl 2019 presentation   multilingualism makes better programmersDrupaljam xl 2019 presentation   multilingualism makes better programmers
Drupaljam xl 2019 presentation multilingualism makes better programmers
 
Angular Data
Angular DataAngular Data
Angular Data
 
PHP: GraphQL consistency through code generation
PHP: GraphQL consistency through code generationPHP: GraphQL consistency through code generation
PHP: GraphQL consistency through code generation
 
Modularity and Layered Data Model
Modularity and Layered Data ModelModularity and Layered Data Model
Modularity and Layered Data Model
 
MVC on the server and on the client
MVC on the server and on the clientMVC on the server and on the client
MVC on the server and on the client
 
Zend Framework 2 - Basic Components
Zend Framework 2  - Basic ComponentsZend Framework 2  - Basic Components
Zend Framework 2 - Basic Components
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Drupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency InjectionDrupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency Injection
 
Having Fun with Play
Having Fun with PlayHaving Fun with Play
Having Fun with Play
 
Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019
 
Typed data in drupal 8
Typed data in drupal 8Typed data in drupal 8
Typed data in drupal 8
 

Models Best Practices (ZF MVC)

  • 1. The 'M' in MVC Zend Framework
  • 2. About Twitter: @eddiejaoude GitHub: eddiejaoude (http://eddiejaoude.github.com) Email: eddie@jaoudestudios.com
  • 3. MVC
  • 4. MVC ● Model - This is the part of your application that defines its basic functionality behind a set of abstractions. Data access routines and some business logic can be defined in the model. ● View - Views define exactly what is presented to the user. Usually controllers pass data to each view to render in some format. Views will often collect data from the user, as well. This is where you're likely to find HTML markup in your MVC applications. ● Controller - Controllers bind the whole pattern together. They manipulate models, decide which view to display based on the user's request and other factors, pass along the data that each view will need, or hand off control to another controller entirely. Most MVC experts recommend » keeping controllers as skinny as possible.
  • 5. Model ● Entity (business object) ● DAO (data access object) ● Mapper (mapping from external to internal) ● Service (NOT web service, business logic)
  • 6. Interface Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled. Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined. All methods declared in an interface must be public, this is the nature of an interface.
  • 7. Interface // Declare the interface 'iTemplate' interface iTemplate { public function setVariable($name, $var); public function getHtml($template); } // Implement the interface class Template implements iTemplate { private $_vars = array(); public function setVariable($name, $var) { $this->_vars[$name] = $var; return $this; } public function getHtml($template) { foreach($this->_vars as $name => $value) { $template = str_replace('{' . $name . '}', $value, $template); } return $template; } }
  • 8. Interface // Declare the interface 'iTemplate' interface iTemplate { /** * Set variable * * @param string $name * @param int $var * * @return iTemplate */ public function setVariable($name, $var); /** * Get html * * @param string $template * * @return string $template */ public function getHtml($template); }
  • 9. Entity class ModelEntitiesUser { /** * @var string */ private $_name = ''; /** * Set variable * * @param string $name * @return ModelEntitiesUser */ public function setName($name) { $this->_name = (string) $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->_name; } }
  • 10. DAO Interface interface ModelDaosUserInterface { /** * Find all * * @return stdClass */ public function findAll(); /** * Find by ID * * @param int $id * * @return stdClass */ public function findById($id); }
  • 11. DAO Database //$_datasource = new ZendDb; class ModelDaosUserDb implements ModelDaosUserInterface { /** * Find all * * @return stdClass */ public function findAll() { return $this->getDatasource()->findAll(); } /** * Find by ID * * @param int $id * * @return stdClass */ public function findById($id) { return $this->getDatasource()->findById($id); } }
  • 12. DAO Soap Client //$_datasource = new ZendSoapClient; class ModelDaosUserSoap implements ModelDaosUserInterface { /** * Find all * * @return stdClass */ public function findAll() { $data = $this>getDatasource()->findAll(); return $data; } /** * Find by ID * * @param int $id * * @return stdClass */ public function findById($id) { $data = $this->getDatasource()->findById(array('id' => 1)); return $data; } }
  • 13. Mapper //$_dao = new ModelDaosUserSoap; //$_dao = new ModelDaosUserDb; class ModelMappersUserDb { /** * Find all * * @return array */ public function findAll() { $users = array(); foreach($this->getDao()->findAll() as $user) { $userEntity = new ModelEntitiesUser; $userEntity->setName($user->name); $users[] = $userEntity; } return $users; } }
  • 14. Service //$_mapper = new ModelMappersUser; class ModelServicesUser { /** * Find all * * @return array */ public function findAll() { // business logic // .. return $this->getMapper()->findAll() ; } }
  • 15. Verbose Usage $dao = new ModelDaosUserSoap; $mapper = new ModelMappersUser($dao); $service = new ModelServicesUser($mapper); $users =$service->findAll(); DIC Dependency Injection Container
  • 16. Service / Mapper Single Service Multiple Mappers Service Mapper DAO (User) (User) (Soap) findAll findEnabled findDisabled
  • 17. Service / Mapper Multiple Services Single Mapper Service Mapper DAO (User) (User) (Soap) findEnabled findAll findDisabled
  • 18. Model Overview Model Datasource DB Interface Entity / Collection Service Mapper DAO API Entity FILE
  • 19. Next Model Datasource Interface DB Interface Entity / Collection Service Mapper DAO API Entity FILE