简体   繁体   English

从应用程序引导程序调用模块控制器

[英]Call Modules Controller from Application Bootstrap

I've asked a question like this previously but I believe this is different (that one was just a general question). 我之前曾问过一个这样的问题,但我相信这是不同的(那只是一个普遍的问题)。

I implemented Zend_Navigation. 我实现了Zend_Navigation。

For menu I used DB Table to store menu items and did recursion on Array-s to get the tree of menu items. 对于菜单,我使用DB Table来存储菜单项,并且对Array-s进行了递归以获得菜单项的树。

All of this action takes place in my module called Menu. 所有这些操作都发生在名为“菜单”的模块中。 Inside I have: 里面我有:

Menu -- 
       Controllers --
                     IndexController.php
       Models--
               DbTable--
                        Menu.php
       Bootstrap.php

inside index controller I have a function menuGenerator($menu_id) 在索引控制器内部,我有一个function menuGenerator($menu_id)

So following tutorials on Zend_Navigation, the menu is initialized in the application bootstrap. 因此,按照Zend_Navigation上的教程,可以在应用程序引导程序中初始化菜单。

my function inside application's bootstrap looks like this: 我的应用程序引导程序中的函数如下所示:

public function _initMenus() {

    $menuArray = new Menu_IndexController();
    $outArray = $menuArray->menuGenerator(1);

    $mainmenu = new Zend_Navigation($outArray);

    $this->view->navigation($mainmenu);     

}

and it gives me an error: 它给了我一个错误:

Fatal error: Class 'Menu_IndexController' not found in D:\Server\xampp\htdocs\project\application\Bootstrap.php on line 8

So, Any ideas how should I make it to work correctly? 那么,有什么想法我应该如何使其正常工作?

PS is it possible to start 2 new menus at a time? PS是否可以一次启动2个新菜单? for ex: I need 1. main menu 2. footer menu (any link to an article would be nice) 例如:我需要1.主菜单2.页脚菜单(指向文章的任何链接都很好)

By default, Zend Framework's autoloader doesn't autoload controllers in the same way it loads other components (models, view helpers, forms, etc), so PHP throws the error saying it can't find the class. 默认情况下,Zend Framework的自动加载器不会以加载其他组件(模型,视图助手,表单等)的相同方式自动加载控制器,因此PHP会抛出错误,指出找不到该类。 The quickest way to get around this is to explicitly include the controller in Bootstrap.php. 解决此问题的最快方法是在Bootstrap.php中明确包含控制器。 The following should work: 以下应该工作:

public function _initMenus() {

    require_once('./Controllers/IndexController.php');
    $menuArray = new Menu_IndexController();
    $outArray = $menuArray->menuGenerator(1);

    $mainmenu = new Zend_Navigation($outArray);

    $this->view->navigation($mainmenu);     

}

It's pretty unusual to call a controller method during Bootstrap since there are many bootstrapping tasks upon which controller actions depend. Bootstrap 期间调用控制器方法是非常不寻常的,因为控制器的动作依赖于许多引导任务。 In your case, the controller method menuGenerator() is not actually an action , so presumably it will not be a problem. 在您的情况下,控制器方法menuGenerator()实际上不是action ,因此大概不会有问题。

Nonetheless, it's still unusual enough that I would move the menuGenerator() method out into its own class. 尽管如此,仍然很不寻常,我将menuGenerator()方法移到其自己的类中。 Then invoke that operation both at Bootstrap and in your controller. 然后在Bootstrap和控制器中调用该操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM