1. 初始文件
1.1 /init_autoload.php -----------------------------------
// Composer autoloading
if (file_exists('vendor/autoload.php')) {
$loader = include 'vendor/autoload.php';
}
if (defined('LIB')) {
include LIB . '/Zend/Loader/AutoloaderFactory.php';
Zend\Loader\AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true
)
));
}
if (!class_exists('Zend\Loader\AutoloaderFactory')) {
throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
}
1.2 /public/index.php ----------------------------------
date_default_timezone_set("UTC");
/**
* This makes our life easier when dealing with paths. Everything is relative
* to the application root now.
*/
chdir(dirname(__DIR__));
//chdir(__DIR__);
if (!defined('APP_PATH'))
define ('APP_PATH', dirname(__DIR__).'/');
if (!defined('LIB'))
define('LIB', APP_PATH . 'vendor/ZF2/library');
// Setup autoloading
require APP_PATH.'init_autoloader.php';
// Run the application!
Zend\Mvc\Application::init(require APP_PATH.'config/application.config.php')->run();
2. 全局变量:不要直接定义全局变量,而应在config/global.php 或 local.php 配置,如
return array(
'service_manager' => array(
'factories' => array(
//'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
'Zend\Db\Adapter\Adapter' => function ($serviceManager) {
$adapterFactory = new Zend\Db\Adapter\AdapterServiceFactory();
$adapter = $adapterFactory->createService($serviceManager);
\Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter($adapter);
return $adapter;
}
),
),
'db' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=testdb;host=localhost',
'username' => 'root',
'password' => '',
),
'msg' => array(
'add' => 'Data Inserted Successfully',
'edit' => 'Data Updated Successfully',
'delete' => 'Data Deleted Successfully',
),
);
?>
Controller File:DemoController.php
<?php
namespace Demo\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class DemoController extends AbstractActionController
{
public function indexAction($cms_page_name='whyus')
{
/*Call config file to fetch current cms page id-- fetch config file from database.local.php*/
$config = $this->getServiceLocator()->get('Config');
$all_msg = $config['msg'];
}
}
?>
3.使用数据表前缀
3.1 方法一, 在操作表时设置
public function __construct(Adapter $adapter, UserEntityInterface $entity) { $resultSet = new ResultSet(); $resultSet->setArrayObjectPrototype($entity); $config = ....?? parent::__construct($config['db']['prefix'].'users', $adapter, null, $resultSet); }
3.2 方法二,全局配置前缀
==== 创建类
======TableNamePrefixFeature
class TableNamePrefixFeature extends Zend\Db\TableGateway\Feature\AbstractFeature
{
protected $prefix = null;
public function __construct($prefix = null)
{
if (null !== $prefix) {
$this->setPrefix($prefix);
}
}
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}
public function postInitialize()
{
if (null !== $this->prefix) {
$this->tableGateway->getSql()->setTable($this->prefix . $this->tableGateway->table);
}
}
}
==== 使用类
=========TableNamePrefixFeature
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'Mysqli',
'database' => 'example',
'username' => '',
'password' => ''
));
$feature = new TableNamePrefixFeature('foo_');
$tableGateway = new Zend\Db\TableGateway\TableGateway('bar', $adapter, $feature);
var_dump($tableGateway->getSql()->getTable()); // string 'foo_bar' (length=7)
$dbParams = array(
'database' => 'wlm',
...
'prefix' => 'wlm_'
);
return array(
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => function ($sm) use ($dbParams) {
return new Zend\Db\Adapter\Adapter(array(
...
));
},
'Zend\Db\TableGateway' => function ($sm) use ($dbParams) {
$dbFeature = new TableNamePrefixFeature($dbParams['prefix']);
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
return new Zend\Db\TableGateway\TableGateway('bar', $dbAdapter, $dbFeature);
},
),
),
);