0
votes

Zend recommends to put controller action helpers in the mvc into /application/controllers/helpers. Is that the right way to use those?

Do I have to instantiate them in the bootstrap/application.ini?

My helper has a direct action and is called Controllers_Helpers_Redirectlogin.

Thanks for your help, I appreciate!

1

1 Answers

2
votes

Following the recommendation is good. By default, it doesn't appear that the autoloader is set up to look in the recommended paths by default even though the manual page you referenced states:

controllers/helpers/: These directories will contain action helpers. Action helpers will be namespaced either as "Controller_Helper_" for the default module or "_Controller_Helper" in other modules.

To get around it, just hint to the HelperBroker the additional paths to look in by adding this to your Bootstrap.php file:

// for default module in a non-modular structure
Zend_Controller_Action_HelperBroker::addPath(
    APPLICATION_PATH . '/controllers/helpers',
    'Controller_Helper'
);

// for default module if using modular structure
Zend_Controller_Action_HelperBroker::addPath(
    APPLICATION_PATH . '/modules/default/controllers/helpers',
    'Controller_Helper'
);

// for "Admin" module when using a modular structure
Zend_Controller_Action_HelperBroker::addPath(
    APPLICATION_PATH . '/modules/admin/controllers/helpers',
    'Admin_Controller_Helper'
);

Also note, for the autoloader to work correctly, you should name your class Controller_Helper_RedirectLogin rather than Controllers_Helpers_RedirectLogin. Usually autoloaded classes drop the plurals (e.g. models/User.php maps to class Model_User).

EDIT: You can also tell the helper broker about the helper paths in the application.ini file as well. Example:

; non-modular structure
resources.frontController.actionHelperPaths.Controller_Helper = APPLICATION_PATH "/controllers/helpers"

; "admin" module in a modular structure
resources.frontController.actionHelperPaths.Admin_Controller_Helper = APPLICATION_PATH "/modules/admin/controllers/helpers"