Formateur et Expert Full Stack
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
View
Controller
Router
Model
Database
Browser
User
<VirtualHost *:80>
DocumentRoot "E:/xampp/htdocs/zendsofrecom/public"
ServerName zendsofrecom.local
</VirtualHost>
Dans le fichier :
xampp/apache/conf/extra/httpd-vhosts
Hello World
CREATE TABLE product (
id int(11) NOT NULL auto_increment,
title varchar(100) NOT NULL,
description varchar(255) NOT NULL,
PRIMARY KEY (id)
);
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;
}
}
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));
}
}
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);
},
),
);
}
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
return array(
'db' => array(
'username' => 'YOUR USERNAME HERE',
'password' => 'YOUR PASSWORD HERE',
),
);
Fichier local.php
// 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;
}