使用Zend中的BaseController扩展IndexController

Bil*_*llA 10 php zend-framework controller class

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

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
}
Run Code Online (Sandbox Code Playgroud)

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

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

mar*_*kus 6

自动加载磁带机

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

//AutoLoad loads classes automatically if they are used
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Mylibrary_');
Run Code Online (Sandbox Code Playgroud)

Zend命名约定

然后,您应该重命名BaseController,如下所示

/Zend (folder)
/Mylibrary (folder)
    /Controller (folder)
        Action.php <-- this is your basecontroller file
Run Code Online (Sandbox Code Playgroud)

其中包含:

class Mylibrary_Controller_Action extends Zend_Controller_Action
{
}
Run Code Online (Sandbox Code Playgroud)

和控制器文件夹中的常规控制器:

class IndexController extends Mylibrary_Controller_Action
{
}
Run Code Online (Sandbox Code Playgroud)

所以基本上当你想扩展框架时,你在自己的库中保持一个并行结构.


che*_*rtz 3

我会将其分离到您自己的库中,即创建文件library/YourApp/Controller/Action.php,并将其命名为YourApp_Controller_Action extends Zend_Controller_Action。从那里,您可以将控制器放置在它们应该在的位置,并让它们扩展YourApp_Controller_Action以支持Zend_Controller_Action.

要查找该文件,您应该依靠自动加载器不仅在库/Zend 内部查找,而且还在库/YourApp 中查找。即在您的引导程序中查找set_include_path

使用这种技术,您应该记住,您的自定义“基本控制器”可能会因并非所有控制器都需要继承的方法而变得臃肿。