2010-09-07 98 views
0

我是Zend框架的新手。Zend框架控制器問題

同時加載我的索引器我得到一個錯誤:

Fatal error: Class 'Places' not found in C:\xampp\htdocs\zend\book\application\controllers\IndexController.php on line 36 

我的引導程序代碼是

<?php 
class Bootstrap 
{ 
    public function __construct($configSection) 
    { 
     $rootDir = dirname(dirname(__FILE__)); 
     define('ROOT_DIR', $rootDir); 
     set_include_path(get_include_path(). PATH_SEPARATOR . ROOT_DIR . '/library/'. PATH_SEPARATOR . ROOT_DIR . 
     '/application/models/'); 

     require_once 'Zend/Loader/Autoloader.php'; 
     $loader = Zend_Loader_Autoloader::getInstance(); 
     // Load configuration 
     Zend_Registry::set('configSection',$configSection); 
     $config = new Zend_Config_Ini(ROOT_DIR.'/application/config.ini',$configSection); 
     Zend_Registry::set('config', $config); 
     date_default_timezone_set($config->date_default_timezone); 
     // configure database and store to the registry 
     $db = Zend_Db::factory($config->db); 
     Zend_Db_Table_Abstract::setDefaultAdapter($db); 
     Zend_Registry::set('db', $db); 
    } 

    public function configureFrontController() 
    { 
     $frontController = Zend_Controller_Front::getInstance(); 
     $frontController->setControllerDirectory(ROOT_DIR .'/application/controllers'); 
    } 

    public function runApp() 
    { 
     $this->configureFrontController(); 
     // run! 
     $frontController = Zend_Controller_Front::getInstance(); 
     $frontController->dispatch(); 
    } 
} 

我有一個模型:

<?php 
class Places extends Zend_Db_Table 
{ 
    protected $_name = 'places'; //table name 
    function fetchLatest($count = 10) 
    { 
     return $this->fetchAll(null,'date_created DESC', $count); 
    } 
} 

我的索引器代碼:

class IndexController extends Zend_Controller_Action 
{ 
    public function indexAction() 
    { 
     $this->view->title = 'Welcome'; 
     $placesFinder = new Places(); 
     $this->view->places = $places->fetchLatest(); 
    } 
} 

我使用ZF版本1.10.4

回答

0

有你缺少somethign在你的類聲明 嘗試一個很好的機會:

<?php 
class Models_Places extends Zend_Db_Table 
{ 
    protected $_name = 'places'; //table name 
    function fetchLatest($count = 10) 
    { 
     return $this->fetchAll(null,'date_created DESC', $count); 
    } 
} 

在Zend自動加載類會考慮型號/地方。 PHP爲你的課程。

你也可以用初始化模式,缺省模塊中引導:

protected function _initAutoload() { 

     $autoloader = new Zend_Application_Module_Autoloader(array(
        'namespace' => '', 
        'basePath' => dirname(__FILE__), 
       )); 
     $autoloader->addResourceType('models', 'models/', 'Models'); 
     return $autoloader; 
    } 

已經做您的類應該被命名爲Models_Places後。

查看有關自動加載的docs

+0

謝謝,不幸的是,它似乎並沒有與您的代碼 – john 2010-09-07 17:14:18

+0

嗯什麼不工作?它應該工作,除非你的代碼中有問題。 – Iznogood 2010-09-07 17:20:08

+0

顯示相同的錯誤,我認爲$ loader上缺少一些東西? – john 2010-09-07 17:21:28

0

那麼,我個人使用擴展控制器,其中包含很少使用我經常使用的util方法。這裏是我的擴展控制器的片段:

<?php 
class My_MyController extends Zend_Controller_Action 
{ 
    protected $_tables = array(); 

    protected function _getTable($table) 
    { 
     if (false === array_key_exists($table, $this->_tables)) { 
      include APPLICATION_PATH . '/modules/' 
      . $this->_request->getModuleName() . '/models/' . $table . '.php'; 
      $this->_tables[$table] = new $table(); 
     } 
     return $this->_tables[$table]; 
    } 
} 

你只需要在index.php中定義APPLICATION_PATH。然後,你的控制器看起來是這樣的:您存儲的My_Controller

<?php 
class IndexController extends My_MyController 
{ 
    public function indexAction() 
    { 
     // get model 
     $model = $this->_getTable('ModelName'); 
    } 
} 

路徑也必須在include路徑。