学习笔记(一)

一.安装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;   }
 

 

转载于:https://www.cnblogs.com/netRob/p/5116371.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值