从 Zend Framework 中的现有控制器类扩展控制器
早上好,我有经典的应用程序,我想从 UserController 扩展 ArticleController,但是当我尝试时
class ArticleController extends UserController
{
// ...
}
,
class ArticleController extends Application_Controllers_UserController
{
}
我得到了致命错误:类...未找到... 如何在 Zend Framework 中从一个控制器扩展另一个控制器?
Good morning, I have classic application, and I'll like to extend ArticleController from UserController, but when I try
class ArticleController extends UserController
{
// ...
}
or
class ArticleController extends Application_Controllers_UserController
{
}
I got Fatal error: class ... not found...
How can I extend one controller from another in Zend Framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
控制器类名的自动加载不是您在应用程序中可以访问或不需要的东西,除非在这种情况下。
您将需要手动包含/需要包含您想要扩展的控制器的文件。
请注意,您的视图脚本仍将从视图/脚本/文章而不是视图/脚本/用户提供。如有必要,您可以调整每个操作中的视图路径。
正如评论中所述,您不必更改 require_once 语句的路径,但您可以根据需要更改它(例如
require_once APPLICATION_PATH . '/modules/test/controllers/UserController.php';)
Autoloading of controller class names is not something you get access to in your application or have much of a need for except in a case like this.
You will need to manually include/require the file that contains the controller you wish to extend.
Note that your view scripts will still be served from views/scripts/article not views/scripts/user. You can adjust the view path in each action if necessary.
As stated in the comment, you shouldn't have to change the path of the require_once statement, but you can change it as necessary (e.g.
require_once APPLICATION_PATH . '/modules/test/controllers/UserController.php';
)您必须先正确初始化自动加载器,然后才能在 Bootstrap 中进行此类操作。特别是当您在 Zend 中的标准控制器目录中扩展控制器时。
现在您可以通过Application_Controller_[YourControllerName]访问它。
如果您有模块化应用程序,您始终可以将“应用程序”替换为您的模块名称或将其留空。
You have to init autoloader properly before you can make such a thing for example in Bootstrap. Esspecialy when you are extending controllers in standard controllers direcotry in Zend.
Now you can access it by Application_Controller_[YourControllerName].
If you have modular application you can allways replace 'Application' with your module name or just leave it blank.