学习笔记(一)
一.安装ZF2框架
方法一:
curl -s https://getcomposer.org/installer | php -- (需要先安装curl,git)
php composer.phar create-project -sdev --repository-url="https://packages.zendframework.com" zendframework/skeleton-application path/to/install
方法二:
cd my/project/dir
git clone git://github.com/zendframework/ZendSkeletonApplication.git
cd ZendSkeletonApplication
php composer.phar self-update
php composer.phar install
测试是否安装成功:
方法一: php5.4+内置服务器,在根目录下运行:php -S 0.0.0.0:8080 -t public/ public/index.php
方法二: httpd.conf中将AllowOverride None改为AllowOverride FileInfo,注意.htaccess,mod_rewrite
二.简单案例
Module.php
namespace Album ;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface ;
use Zend\ModuleManager\Feature\ConfigProviderInterface ;
class Module implements AutoloaderProviderInterface , ConfigProviderInterface
{
public function getAutoloaderConfig ()
{
return array (
'Zend\Loader\ClassMapAutoloader' => array (
__DIR__ . '/autoload_classmap.php' ,
),
'Zend\Loader\StandardAutoloader' => array (
'namespaces' => array (
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__ ,
),
),
);
}
public function getConfig ()
{
return include __DIR__ . '/config/module.config.php' ;
}
}
建立一个模型
namespace Album\Model; use Zend\Db\TableGateway\TableGateway; class AlbumTable { protected $tableGateway; public function __construct(TableGateway $tableGateway) { $this->tableGateway = $tableGateway; } public function fetchAll() { $resultSet = $this->tableGateway->select(); return $resultSet; } public function getAlbum($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 saveAlbum(Album $album) { $data = array( 'artist' => $album->artist, 'title' => $album->title, ); $id = (int) $album->id; if ($id == 0) { $this->tableGateway->insert($data); } else { if ($this->getAlbum($id)) { $this->tableGateway->update($data, array('id' => $id)); } else { throw new \Exception('Album id does not exist'); } } } public function deleteAlbum($id) { $this->tableGateway->delete(array('id' => (int) $id)); } }
$sm的用武之地
namespace Album;
// Add these import statements:
use Album\Model\Album; use Album\Model\AlbumTable; use Zend\Db\ResultSet\ResultSet; use Zend\Db\TableGateway\TableGateway; class Module { // getAutoloaderConfig() and getConfig() methods here // Add this method: public function getServiceConfig() { return array( 'factories' => array( 'Album\Model\AlbumTable' => 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('album', $dbAdapter, null, $resultSetPrototype); }, ), ); } }
return array(
'db' => array( 'driver' => 'Pdo', 'dsn' => 'mysql:dbname=zf2tutorial;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', ), ), );
public function getAlbumTable()
{
if (!$this->albumTable) {
$sm = $this->getServiceLocator();
$this->albumTable = $sm->get('Album\Model\AlbumTable');
}
return $this->albumTable;
}