2

是否可以在application.ini中配置以下行爲?Zend Framework:自動加載config.ini中的模塊資源?

<?php 
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    protected function _initAdminModuleAutoloader() 
    { 

    $this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
     'namespace' => 'Admin', 
     'basePath' => APPLICATION_PATH . '/modules/admin', 
    )); 
     $this->_resourceLoader->addResourceTypes(array(
      'model' => array(
       'namespace' => 'Model', 
       'path'  => 'models' 
      ) 
     )); 
} 

} 
?> 

如果是這樣,請你給我們看一個例子嗎?

謝謝。

+0

該示例有用嗎?你有看到它嗎? – Travis 2010-03-15 03:02:13

+0

也有同樣的問題。特拉維斯你的回答不回答這個問題。 – sanders 2010-08-05 12:46:18

回答

2

這裏有幾個指針。首先你應該在你的application.ini文件中有下面的行。

resources.modules.admin = "enabled" 

這將確保在Zend_Application_Resource_Modules以下函數運行

public function init() 
{ 
    $bootstrap = $this->getBootstrap(); 
    $bootstrap->bootstrap('FrontController'); 
    $front = $bootstrap->getResource('FrontController'); 

    $modules = $front->getControllerDirectory(); 
    $default = $front->getDefaultModule(); 
    $curBootstrapClass = get_class($bootstrap); 
    foreach ($modules as $module => $moduleDirectory) { 
     $bootstrapClass = $this->_formatModuleName($module) . '_Bootstrap'; 
     if (!class_exists($bootstrapClass, false)) { 
      $bootstrapPath = dirname($moduleDirectory) . '/Bootstrap.php'; 
      if (file_exists($bootstrapPath)) { 
       $eMsgTpl = 'Bootstrap file found for module "%s" but bootstrap class "%s" not found'; 
       include_once $bootstrapPath; 
       if (($default != $module) 
        && !class_exists($bootstrapClass, false) 
       ) { 
        throw new Zend_Application_Resource_Exception(sprintf(
         $eMsgTpl, $module, $bootstrapClass 
        )); 
       } elseif ($default == $module) { 
        if (!class_exists($bootstrapClass, false)) { 
         $bootstrapClass = 'Bootstrap'; 
         if (!class_exists($bootstrapClass, false)) { 
          throw new Zend_Application_Resource_Exception(sprintf(
           $eMsgTpl, $module, $bootstrapClass 
          )); 
         } 
        } 
       } 
      } else { 
       continue; 
      } 
     } 

     if ($bootstrapClass == $curBootstrapClass) { 
      // If the found bootstrap class matches the one calling this 
      // resource, don't re-execute. 
      continue; 
     } 

     $moduleBootstrap = new $bootstrapClass($bootstrap); 
     $moduleBootstrap->bootstrap(); 
     $this->_bootstraps[$module] = $moduleBootstrap; 
    } 

    return $this->_bootstraps; 
} 

在上面的函數你的模塊引導被調用。你必須在/應用/模塊/管理模塊引導文件稱爲bootstrap.php中使用下面的代碼:

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap 
{ 


} 

我要跳過了幾步,但如果你通過繼承的類跟蹤這將導致下面的函數被調用的Zend_Application_Module_Autoloader

public function initDefaultResourceTypes() 
{ 
    $basePath = $this->getBasePath(); 
    $this->addResourceTypes(array(
     'dbtable' => array(
      'namespace' => 'Model_DbTable', 
      'path'  => 'models/DbTable', 
     ), 
     'mappers' => array(
      'namespace' => 'Model_Mapper', 
      'path'  => 'models/mappers', 
     ), 
     'form' => array(
      'namespace' => 'Form', 
      'path'  => 'forms', 
     ), 
     'model' => array(
      'namespace' => 'Model', 
      'path'  => 'models', 
     ), 
     'plugin' => array(
      'namespace' => 'Plugin', 
      'path'  => 'plugins', 
     ), 
     'service' => array(
      'namespace' => 'Service', 
      'path'  => 'services', 
     ), 
     'viewhelper' => array(
      'namespace' => 'View_Helper', 
      'path'  => 'views/helpers', 
     ), 
     'viewfilter' => array(
      'namespace' => 'View_Filter', 
      'path'  => 'views/filters', 
     ), 
    )); 
    $this->setDefaultResourceType('model'); 
} 

上述所有資源類型將默認爲您遵循這個模式的每一個模塊覆蓋。您需要的任何其他人都需要在引導程序文件中進行自定義。

+0

謝謝特拉維斯。這是一個好方法! 我想簡單的東西是這樣的: resources.frontcontroller.namespaces.viewfilter.ns =「View_Filter」 resources.frontcontroller.namespaces.viewfilter.path = 「的意見/過濾器」 我想這樣的配置Zend_Application尚不可用。也許在更近的將來.... – olagato 2010-03-16 16:27:57

+0

那麼這很簡單。 你只需要行 resources.modules.admin =「啓用」 和空引導文件。 Zend_Application負責爲你休息。我只是通過爲您設置的實際Zend代碼來默認設置它。 – Travis 2010-03-16 16:49:16

0

我使用的Zend框架版本1.10.4與默認的項目配置。我激活我的application.ini所有模塊使用以下行:

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" 

其重要的是你在你的模塊目錄中有一個bootstrap.php中。沒有這個,我的模型沒有正確加載。瀏覽器中的網址似乎區分大小寫。例如:http://example.com/Admin/controller/action

因此,它應該工作...