Unable to access module forms in module controller

2019-08-12 13:42发布

I have a test module. In test module I have a Form in forms folder.

myproject/application/modules/test/forms/TestForm.php

class Test_Form_TestForm extends Zend_Form {
    //form elements
}

myproject/application/modules/test/controllers/TestController.php

class Test_TestController extends Zend_Controller_Action {

    public function indexAction() {
        $this->view->form = new Test_Form_TestForm();  // this is generating error
    }
} // end class

Form initialization in controller is generating following error:

Fatal error: Class 'Test_Form_TestForm' not found in C:\wamp\www\student\application\modules\notification\controllers\NotificationController.php on line 16

How to make this form accessable in controller. Same type of case is working with default controller. I know I have to register my module in bootstrap with Form_ indicator but dont know exact syntax.

5条回答
The star\"
2楼-- · 2019-08-12 14:02

I don't know if this is the best way, but it works.

In your bootstrap

...
$autoloader = new Zend_Loader_Autoloader_Resource(array('namespace' => '', 'basePath' => APPLICATION_PATH));
$autoloader->addResourceType('Test_Form', '/test/forms', 'Test_Form');
...
查看更多
乱世女痞
3楼-- · 2019-08-12 14:05

In order for Zend Autoloader to work for your modules, you need to have bootstraps for all of your modules, and also modules resource initialized.

So, in your application/modules/test/Bootstrap.php:

class Test_Bootstrap extends Zend_Application_Module_Bootstrap {}

Upd:

And in your application/configs/application.ini:

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"    
resources.modules[] = 

More info about autoloading in modules here

查看更多
劫难
4楼-- · 2019-08-12 14:05

Vika's answer seems to be correct.

If you still having problems, try modify your application.ini

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.moduleDefault = "test"
resources.modules[] = "test"
resources.modules[] = "other"

If you specify exact module names in resource list, Zend will auto-magically register the Form and other resource auto-loaders. In debugging case modules/test/Boostrap.php should be triggered and any _init method inside. Have fun.

查看更多
姐就是有狂的资本
5楼-- · 2019-08-12 14:11

Vika's answer is correct on how to setup modules autoloader.

Your error states that the form class cannot be found in notification module under NotificationController controller.

So you need to have bootstrap class for the notification module

In your application/modules/notification/Bootstrap.php:

class Notification_Bootstrap extends Zend_Application_Module_Bootstrap {}
查看更多
唯我独甜
6楼-- · 2019-08-12 14:12

You can also initialize multiple modules in a separate function in one Bootstrap file like:

protected function _initAutoloaders() {

        $test_loader = new Zend_Application_Module_Autoloader( array(   'namespace' => 'Test',
                                                                            'basePath'  => APPLICATION_PATH . '/modules/test'
        ));


      $mynew_loader = new Zend_Application_Module_Autoloader( array(    'namespace' => 'Mynew',
                                                                            'basePath'  => APPLICATION_PATH . '/modules/mynew'
        ));
}
查看更多
登录 后发表回答