简体   繁体   中英

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.

For menu I used DB Table to store menu items and did recursion on Array-s to get the tree of menu items.

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)

So following tutorials on Zend_Navigation, the menu is initialized in the application bootstrap.

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? for ex: I need 1. main menu 2. footer menu (any link to an article would be nice)

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. The quickest way to get around this is to explicitly include the controller in 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. In your case, the controller method menuGenerator() is not actually an action , so presumably it will not be a problem.

Nonetheless, it's still unusual enough that I would move the menuGenerator() method out into its own class. Then invoke that operation both at Bootstrap and in your controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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