Zend Framework Bootstrap Issue

不问归期 提交于 2019-12-09 19:39:41

问题


I have been working on a new installation of a Zend Framework application for a while now, and I cannot figure out what's going on. I have two custom action helpers I would like to use and I would like to initialize those in the bootstrap. But it seems as though my _init functions are not being called at all. In the index.php that starts the application I have:

require('Zend/Application.php');

$app = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH 
.'/configs/application.ini');

$app->bootstrap()->run();

Here's what I have in the application.ini file:

[production]

appnamespace = "Application_Name"

includePaths.library = APPLICATION_PATH "/../library"

bootstrap.path = "/home/user/website/includes/library/Application_Name/Resource/Bootstrap.php"

bootstrap.class = "Bootstrap"

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

resources.view[] =

autoloaderNamespaces[] = "Application_Name"

pluginPaths.Application_Name_Resource = "Application_Name/Resource"

I know the application is somewhat working because it is using a layout that I have and I can do things in the controllers and views and have it output to the page. I also know that it is at least looking at the Bootstrap file because I can make a PHP error happen when I leave out an end curly brace.

Here's a portion of my Bootstrap file:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

        public function _init()
        {
                Zend_Controller_Action_HelperBroker::addPrefix(new Application_Name_Controller_Action_Helper_ResourceInjector());
                Zend_Controller_Action_HelperBroker::addPrefix(new Application_Name_Controller_Action_Helper_Em());
        }

Any ideas why this would be or see something that I've messed up in my configuration? I've looked at tens of tutorials on how to configure Zend, and no one else seems to have this problem.


回答1:


You're not using the helper broker correctly. addPrefix() is used to add pluginloader prefix paths, not actual classes.

If you want to add concrete helpers (to use their dispatch hooks presumably), then place something like this in your Bootstrap class

protected function _initActionHelpers()
{
    $helper = new My_Helper;
    Zend_Controller_Action_HelperBroker::addHelper($helper);
}

For regular, runtime helpers, you can easily add prefix paths in your config, eg

resources.frontController.actionHelperPaths.ProEquipTrack_Controller_Action_Helper = "ProEquipTrack/Controller/Action/Helper"

These will be automatically loaded by the broker at call time, eg (controller context)

$resourceInjector = $this->getHelper('ResourceInjector');
$em = $this->getHelper('Em');

or using the strategy pattern (direct() method)

$this->_helper->resourceInjector($arg1, $arg2 /*, etc */);

Doctrine Entity Manager

Do something like this in your Bootstrap class

protected function _initDoctrine()
{
    // initialise and create entity manager
    $em = // whatever

    return $em;
}

You can now access the entity manager in your controllers using this

$em = $this->getInvokeArg('bootstrap')
           ->getResource('doctrine');


来源:https://stackoverflow.com/questions/6247605/zend-framework-bootstrap-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!