在 Zend 中使用 BaseController 扩展 IndexController

2022-01-05 00:00:00 class php zend-framework controller

我正在尝试使用全局基本控制器来扩展我的控制器:

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
}

但我收到此错误:PHP 致命错误:在第 3 行的/var/www/Zend/project/application/controllers/IndexController.php 中找不到类BaseController"

But I get this error: PHP Fatal error: Class 'BaseController' not found in /var/www/Zend/project/application/controllers/IndexController.php on line 3

关于如何让 Zend看到"这个控制器的任何想法?

Any ideas on how to get Zend to "see" this controller?

推荐答案

Autoloader

设置自动加载器并使用自动加载器注册除了 Zend 库之外的库(在设置包含路径后在 bootstrap.php 中):

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 命名约定

那么你应该按如下方式重命名你的 BaseController

Zend naming conventions

Then you should rename your BaseController as follows

/Zend (folder)
/Mylibrary (folder)
    /Controller (folder)
        Action.php <-- this is your basecontroller file

其中包含:

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.

相关文章