程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> zend framework如何實現多模塊化

zend framework如何實現多模塊化

編輯:PHP基礎知識
 

雖然有MVC的架構了,但在開發中,網站肯定會分多個模塊,如分front,admin等模塊

進入入口文件index.php,修改下面內容,將models加入include_path,用於models的類的自動查找加載

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH . '/models'),
get_include_path(),)));

進入application\configs修改application.ini,這次的改動較大,下面是改好後的全部內容

[production];=========== php ini配置
phpSettings.date.timezone = "Asia/Shanghai"
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
phpsettings.error_reporting = 8191;=========== 庫文件路徑
includePaths.library = APPLICATION_PATH "/../library";=========== 類自動加載的前綴
autoloadernamespaces.0 = "front_"
autoloadernamespaces.1 = "admin_";=========== bootstrap類的路徑及類名
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap";=========== 自定義資源的路徑及其前綴;這裡用了自定義資源,需要新建一個類文件view.php,內容下面再說
pluginPaths.Resource = APPLICATION_PATH "/../library/Resource/";=========== front controller配置;appnamespace = "Application";resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0

resources.frontController.moduleDirectory = APPLICATION_PATH "/control/"
resources.frontController.moduleControllerDirectoryName = "controllers"
resources.frontController.defaultModule = "front"
resources.frontController.noErrorHandler = 0
resources.frontController.throwExceptions = 1;=========== 自定義拓展插件,名為module;這裡用了自定義插件,需要新建一個類文件module.php,內容下面再說
resources.frontController.plugins.module = "Zend_Controller_Plugin_Module";=========== view 配置
resources.view.title = ""
resources.view.encoding = "UTF-8"
resources.view.helperPathPrefix = "Views_Helpers_"
resources.view.helperPath = "views/helpers/";=========== front和admin模塊的view參數,包括scripts文件路徑及前綴
resources.view.params.front.basePath = APPLICATION_PATH "/views/front/"
resources.view.params.front.helperPathPrefix = "Views_Helpers_Front_"
resources.view.params.front.helperPath = "Views/Helpers/Front/"

resources.view.params.admin.basePath = APPLICATION_PATH "/views/admin/"
resources.view.params.admin.helperPathPrefix = "Views_Helpers_Admin_"
resources.view.params.admin.helperPath = "Views/Helpers/Admin/";=========== view的其它參數
resources.view.params.pathCss = "/public/css/"
resources.view.params.pathImg = "/public/img/"
resources.view.params.pathJs = "/public/js/"
resources.view.params.doctype = "HTML4_STRICT"
resources.view.params.charset = "utf-8";=========== 數據庫配置
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = root
resources.db.params.dbname = cms
resources.db.params.charset = utf8
resources.db.isDefaultTableAdapter = true
resources.db.params.driver_options.1002 = "SET NAMES UTF8;"[staging : production][testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
ini的內容到此結束

用自定義資源初始化view,創建類view.php

class Resource_View extends Zend_Application_Resource_ResourceAbstract{
protected $_view;

// 初始化view
public function init()
{
if (null === $this->_view) {
// 獲取從Application.ini中的配置
$options = $this->getOptions();
$view = new Zend_View($options);
if (!empty($options['params'])) {
foreach ($options['params'] as $key => $value) {
$view->$key = $value;
}
}

// viewRenderer動作助手
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);

// 保存配置好的視圖對象
$viewRenderer->setView($view);
$this->_view = $view;
}
return $this->_view;
}}
並把view.php文件放於library\Resource下

使用自定義插件加載配置view,創建類module.php

class Zend_Controller_Plugin_Module extends Zend_Controller_Plugin_Abstract{
// route結束時
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
// 獲取模塊名,如admin,front等
$module = $request->getModuleName();

// bootstrap類
$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');

// 加載view
$bootstrap->bootstrap('View');
$view = $bootstrap->getResource('View');
$moduleParams = $view->$module;

// 配置view
$view->addBasePath($moduleParams['basePath'])
->addHelperPath($moduleParams['helperPath'],
$moduleParams['helperPathPrefix']);

}}
並把module.php文件放於library\Zend\Controller\Plugin下

在application文件夾下,建立models,views,control三個文件夾,並在裡面創建相應模塊的文件夾front,admin,默認模塊是front
將之前的controllers的文件復制到application\control\front下
再將之前views的scripts復制到application\views\front下

現在在models\front下創建類class.php

class front_class extends Zend_Db_Table{
protected $_name ="class";
protected $_primary = 'id';
public $_db;

public function init(){
$this->_db = $this->getAdapter();
}}
進入application\control\front\controllers的IndexController.php,在indexAction()裡寫入下面內容
$class=new front_class();
$this->view->class_str = $class->fetchAll()->toArray();

到這裡修改完成,保存文件,刷新頁面,查看顯示結果
mvc下的文件也都分模塊位置存放了,在管理上也方便很多了

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved