【问题标题】:How to use a service class in a controller class?如何在控制器类中使用服务类?
【发布时间】:2011-12-09 02:23:35
【问题描述】:

我正在尝试将this amazing calendar 集成到我的项目中。我在服务方面遇到了麻烦。我正在使用这种结构:

application/
    modules/
        agenda/
            controllers/
                CalendarController.php
            models/
            services/
                CalendarService.php
            views/
                filters/
                helpers/
                scripts/
                calendar/
                    view-calendar.php
    module2/
    module3/

我以这种方式在我的 application.ini 中路由它:

resources.router.routes.agenda.route = /apps/agenda/
resources.router.routes.agenda.defaults.module = agenda
resources.router.routes.agenda.defaults.controller = calendar
resources.router.routes.agenda.defaults.action = view

我打开浏览器,我收到了这条消息:

Message: Invalid controller class ("Agenda_CalendarController") 

我用我的 Agenda_ 后缀修复了它并刷新了我的浏览器。得到这个:

Message: Controller "Agenda_CalendarController" is not an instance of Zend_Controller_Action_Interface

所以我用“扩展 Zend_Controller_Action”来实例化它。得到这个::-)

Fatal error: Class 'Service_Calendar' not found in /home/[PATH TO MY APP FOLDER]/application/modules/agenda/controllers/CalendarController.php on line 30

我不知道出了什么问题。有人给我一两个提示:-)

注意:我不知道这是否意味着什么,但我使用的是 NetBeans,如果我在将服务类方法悬停在控制器类中时按下 CTRL;相关弹窗信息正确。

解决方案: 谢谢你们,你们给了我更进一步的道路。我的模块引导设置不正确。根据您的提示,我从 Bob Allen 那里找到了这篇文章,他在其中描述了同样的问题。

当您知道要搜索什么时... :-)

谢谢,我的问题已经解决了,我知道的更多了!

【问题讨论】:

  • 尝试将application/modules/agenda/services/CalendarService.php 移动到application/services/Calendar.php,看看是否有运气找到课程。您可能需要使用您的应用程序命名空间(例如 Application_Service_Calendar)重命名该类。

标签: php oop zend-framework class zend-framework-mvc


【解决方案1】:

控制器的命名约定与模型/服务/视图等不同,因为您不需要像使用控制器那样在文件名中添加类型。它们必须遵循基本的自动加载名称到路径模式。

因此,为您服务的声明将是:Agenda_Service_CalendarService,因为它映射到 modules/agenda/services/CalendarService.php(注意变形 - 从 services 文件夹到 _Service_ 单数 - 这是 ZF 工作的,它发生在模型上,也是)。

还要确保您已正确初始化模块(拥有自己的 Bootstrap.php 文件,在 application.ini 中设置)。

PS:NetBeans 知道您的类在哪里,因为它扫描了文件并看到了声明,而不是该类的逻辑包含。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多