前置作業
先參考前一篇,把 skeleton (骨架) application 的建立
我們遵照線上文件,把 Module 建立起來,我們建立一個 Module 名稱為 Album 其目錄架構如下:
project-name/
—–/module
———-/Album
—————/config
—————/src
——————–/Album
————————-/Controller
————————/Form
————————/Model
—————/view
——————–/album
————————/album
由上面的目錄架構得知,最上層的專案根目錄為:project-name 而 module 是下一層目錄,接下來是 Album 我們取的目錄名稱,再下一層有 config, src 以及 view 這三個目錄。
而 src 目錄下又有 Album 目錄,Album 目錄下又有 Controller, From 以及 Model,而 view 目錄下面有一層 album 再下一層有 album。
Module.php
建立一個 PHP 檔名為 Module.php 把它放在:/path/to/project-name/module/Album/Module.php 程式碼如下:
<?php | |
namespace Album; | |
use Zend\ModuleManager\Feature\AutoloaderProviderInterface; | |
use Zend\ModuleManager\Feature\ConfigProviderInterface; | |
use Album\Model\Album; | |
use Album\Model\AlbumTable; //後來新增 | |
use Zend\Db\ResultSet\ResultSet; //後來新增 | |
use Zend\Db\TableGateway\TableGateway; //後來新增 | |
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'; | |
} | |
//getServiceConfig 為後來新增 | |
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); | |
}, | |
), | |
); | |
} | |
} | |
?> |
Autoloading files
自動載入用,因為我們在開發的階段,因此只要建立一個 PHP 檔,其路徑:/path/to/project-name/module/Album/autoload_classmap.php 程式碼如下:
1 | <?php return array (); ?> |
Module Configuration
建立一個 module.config.php 並放在:/path/to/project-name/module/Album/config 程式碼如下:
<?php | |
return array( | |
'controllers' => array( | |
'invokables' => array( | |
), | |
), | |
'view_manager' => array( | |
'template_path_stack' => array( | |
'album' => __DIR__ . '/../view', | |
), | |
), | |
); | |
?> |
Informing the application about our new module
修改 application.config.php 其位置在:/path/to/project-name/config/application.config.php 加入一行 ‘Album’ 做為啟用 Album 模組
<?php | |
return array( | |
// This should be an array of module namespaces used in the application. | |
'modules' => array( | |
'Application', | |
'Album' //增加這一行 | |
), | |
// These are various options for the listeners attached to the ModuleManager | |
'module_listener_options' => array( | |
// This should be an array of paths in which modules reside. | |
// If a string key is provided, the listener will consider that a module | |
// namespace, the value of that key the specific path to that module's | |
// Module class. | |
'module_paths' => array( | |
'./module', | |
'./vendor', | |
), | |
// An array of paths from which to glob configuration files after | |
// modules are loaded. These effectively override configuration | |
// provided by modules themselves. Paths may use GLOB_BRACE notation. | |
'config_glob_paths' => array( | |
'config/autoload/{,*.}{global,local}.php', | |
), | |
// Whether or not to enable a configuration cache. | |
// If enabled, the merged configuration will be cached and used in | |
// subsequent requests. | |
//'config_cache_enabled' => $booleanValue, | |
// The key used to create the configuration cache file name. | |
//'config_cache_key' => $stringKey, | |
// Whether or not to enable a module class map cache. | |
// If enabled, creates a module class map cache which will be used | |
// by in future requests, to reduce the autoloading process. | |
//'module_map_cache_enabled' => $booleanValue, | |
// The key used to create the class map cache file name. | |
//'module_map_cache_key' => $stringKey, | |
// The path in which to cache merged configuration. | |
//'cache_dir' => $stringPath, | |
// Whether or not to enable modules dependency checking. | |
// Enabled by default, prevents usage of modules that depend on other modules | |
// that weren't loaded. | |
// 'check_dependencies' => true, | |
), | |
// Used to create an own service manager. May contain one or more child arrays. | |
//'service_listener_options' => array( | |
// array( | |
// 'service_manager' => $stringServiceManagerName, | |
// 'config_key' => $stringConfigKey, | |
// 'interface' => $stringOptionalInterface, | |
// 'method' => $stringRequiredMethodName, | |
// ), | |
// ) | |
// Initial configuration with which to seed the ServiceManager. | |
// Should be compatible with Zend\ServiceManager\Config. | |
// 'service_manager' => array(), | |
); |