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