Zend框架控制器的问题

时间:2010-09-07 17:04:02

标签: php zend-framework

我是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

2 个答案:

答案 0 :(得分:0)

你很有可能在课堂宣言中缺少某些东西 尝试:

   <?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自动加载器类将查看您的类的Models / places.php。

您还可以使用以下命令初始化bootstrap中的模型和默认模块:

protected function _initAutoload() {

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

完成后,您的类应命名为Models_Places。

查看有关自动加载的docs

答案 1 :(得分: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即可。然后您的控制器可能如下所示:

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

存储My_Controller的路径也必须位于包含路径中。