Php Zend框架控制器的问题
我是Zend框架的新手 加载索引控制器时出错:Php Zend框架控制器的问题,php,zend-framework,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) { $roo
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();
}
}
我使用的是ZF版本1.10.4您很可能在类声明中遗漏了一些内容 尝试: 完成后,您的类应命名为Models\u 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];
}
}
谢谢,不幸的是,它似乎不适用于您的代码。什么不适用?除非您的代码中有问题,否则它应该可以工作。如果显示相同的错误,我认为$loader上缺少一些内容?我可以通过添加include('Places.php')来解决这个问题;在IndexController的顶部,是否有任何方法可以避免此包含,我想自动加载它阅读我链接的关于autoloader的内容。正常情况下,如果你像我展示的那样在引导中初始化它,除非你的文件夹组织不同,否则它应该可以工作。
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->title = 'Welcome';
$placesFinder = new Places();
$this->view->places = $places->fetchLatest();
}
}
<?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);
}
}
protected function _initAutoload() {
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => dirname(__FILE__),
));
$autoloader->addResourceType('models', 'models/', 'Models');
return $autoloader;
}
<?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];
}
}
<?php
class IndexController extends My_MyController
{
public function indexAction()
{
// get model
$model = $this->_getTable('ModelName');
}
}