在Zend中使用BaseController扩展IndexController [英] Extending the IndexController with a BaseController in Zend
问题描述
类BaseController扩展了Zend_Controller_Action {
//公共控制器动作
public function listAction(){
// do stuff
}
}
类IndexController扩展BaseController {
//索引控制器特定操作
}
class LoginController extends BaseController {
//登录控制器特定操作
}
$ c $但是我得到这个错误:
PHP致命错误:类'BaseController'没有在/ var / www / Zend / project中找到/ application / controllers / IndexController.php on line 3
有关如何让Zend看到这个控制器的任何想法?
解决方案
自动加载器
设置自动加载器,注册你的库,在你的bootstrap.php中设置包含路径后):
// AutoLoad自动加载类如果使用
require_once'Zend / Loader / Autoloader.php';
$ loader = Zend_Loader_Autoloader :: getInstance();
$ loader-> registerNamespace('Mylibrary_');
Zend命名惯例
重命名BaseController如下
/ Zend(文件夹)
/ Mylibrary(文件夹)
/文件夹)
Action.php< - 这是您的basecontroller文件
class Mylibrary_Controller_Action extends Zend_Controller_Action
{
}
和控制器文件夹中的正常控制器:
class IndexController extends Mylibrary_Controller_Action
{
}
您在自己的库中保留并行结构的框架。
I'm trying to extend my controllers with a global base controller as such:
class BaseController extends Zend_Controller_Action {
// common controller actions
public function listAction() {
// do stuff
}
}
class IndexController extends BaseController {
// index controller specific actions
}
class LoginController extends BaseController {
// login controller specific actions
}
But I get this error:
PHP Fatal error: Class 'BaseController' not found in /var/www/Zend/project/application/controllers/IndexController.php on line 3
Any ideas on how to get Zend to "see" this controller?
解决方案 Autoloader
Setup the autoloader and register your library which should be besides the Zend library with the autoloader like so (in your bootstrap.php after setting the include path):
//AutoLoad loads classes automatically if they are used
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Mylibrary_');
Zend naming conventions
Then you should rename your BaseController as follows
/Zend (folder)
/Mylibrary (folder)
/Controller (folder)
Action.php <-- this is your basecontroller file
which contains:
class Mylibrary_Controller_Action extends Zend_Controller_Action
{
}
and your normal controllers in the controller folder:
class IndexController extends Mylibrary_Controller_Action
{
}
so basically when you want to extend the framework you keep a parallel structure in your own library.
这篇关于在Zend中使用BaseController扩展IndexController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!