如何在控制器类中使用服务类?

发布于 2024-12-20 08:15:17 字数 1486 浏览 1 评论 0原文

我正在尝试集成

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

所以我用“extends 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) 的这篇文章,他在其中描述了同样的问题。

当您知道自己在寻找什么时...:-)

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

I'm trying to integrate this amazing calendar in my project. I get trouble with the service thing. I'm using this kind of structure:

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

I route this in my application.ini this way:

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

I pointed my browser on, and I obtained this message:

Message: Invalid controller class ("Agenda_CalendarController") 

I fixed it with my Agenda_ suffix and refreshed my browser. Obtained this:

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

So I instanced it with "extends Zend_Controller_Action". Obtained this: :-)

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

I can't figure out what's going wrong. Someone have a hint or two for me :-)

Note: I do not know if that means anything, but I use NetBeans and if I press CTRL while hover a class method of service in the controller class; related popup informations are correct.

Solution:
Thanks guys, You gave me the path to go further. My modules bootstraping wasn't set properly. with your hints I found this post from Bob Allen, where he describes the same problem.

When you know what you're searching for... :-)

Thanks, my problem is fixed and I know a bit more!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少跟Wǒ拽 2024-12-27 08:15:17

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

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

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

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

The naming convention for a controller isn't the same for model/service/view etc., in the sense that you needn't prepend the type in the filename, as you would with a controller. They must follow the basic autoloading name-to-path schema.

So, the declaration for you service would be: Agenda_Service_CalendarService, because that maps to modules/agenda/services/CalendarService.php (note the inflection - from the services folder to the _Service_ singular - that's ZF working and it happens for models, too).

Also make sure you've properly initialized the module (having its own Bootstrap.php file, set up in application.ini).

PS: NetBeans knows where your class is, because it scanned the files and saw the declaration, not the logical inclusion of that class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文