繁体   English   中英

Zend框架控制器的问题

[英]Issue with Zend framework controller

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

您很有可能在类声明尝试中缺少某些东西:

   <?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 autoloader类将为您的类查找Models / places.php。

您还可以使用以下方法在引导程序中初始化模型和默认模块:

protected function _initAutoload() {

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

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

查看有关自动加载的文档

好吧,就我个人而言,我使用扩展控制器,其中包含很少使用的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的路径也必须在您的包含路径中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM