Zend框架Zend_Controller_Action_HelperBroker :: addPath不起作用

时间:2011-06-30 14:10:32

标签: php zend-framework

无论使用何种路径,我都会收到此消息,即使我将类文件放在默认目录位置也是如此。

Zend_Controller_Action_HelperBroker::addPath('./Plugins/Helpers','Helper');

Sorry, An error has occured: Application Error:exception 'Zend_Loader_PluginLoader_Exception' with message 'Plugin by name 'FormLoader' was not found in the registry; used paths:
Zend_Controller_Action_Helper_: Zend/Controller/Action/Helper/' in C:\PHP\Zendframework\Zend\Loader\PluginLoader.php:412
Stack trace:
#0 C:\PHP\Zendframework\Zend\Controller\Action\HelperBroker.php(366): Zend_Loader_PluginLoader->load('FormLoader')
#1 C:\PHP\Zendframework\Zend\Controller\Action\HelperBroker.php(293): Zend_Controller_Action_HelperBroker::_loadHelper('FormLoader')
#2 C:\PHP\Zendframework\Zend\Controller\Action\HelperBroker.php(323): Zend_Controller_Action_HelperBroker->getHelper('formLoader')
#3 D:\websites\maklare.easypic.se\appfiles\application\controllers\UserController.php(13): Zend_Controller_Action_HelperBroker->__call('formLoader', Array)
#4 D:\websites\maklare.easypic.se\appfiles\application\controllers\UserController.php(13): Zend_Controller_Action_HelperBroker->formLoader('login')
#5 C:\PHP\Zendframework\Zend\Controller\Action.php(513): UserController->indexAction()
#6 C:\PHP\Zendframework\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('indexAction')
#7 C:\PHP\Zendframework\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#8 C:\PHP\Zendframework\Zend\Controller\Front.php(212): Zend_Controller_Front->dispatch()
#9 D:\websites\maklare.easypic.se\index.php(23): Zend_Controller_Front::run('D:\websites\mak...')
#10 {main}

Next exception 'Zend_Controller_Action_Exception' with message 'Action Helper by name FormLoader not found' in C:\PHP\Zendframework\Zend\Controller\Action\HelperBroker.php:369
Stack trace:
#0 C:\PHP\Zendframework\Zend\Controller\Action\HelperBroker.php(293): Zend_Controller_Action_HelperBroker::_loadHelper('FormLoader')
#1 C:\PHP\Zendframework\Zend\Controller\Action\HelperBroker.php(323): Zend_Controller_Action_HelperBroker->getHelper('formLoader')
#2 D:\websites\maklare.easypic.se\appfiles\application\controllers\UserController.php(13): Zend_Controller_Action_HelperBroker->__call('formLoader', Array)
#3 D:\websites\maklare.easypic.se\appfiles\application\controllers\UserController.php(13): Zend_Controller_Action_HelperBroker->formLoader('login')
#4 C:\PHP\Zendframework\Zend\Controller\Action.php(513): UserController->indexAction()
#5 C:\PHP\Zendframework\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('indexAction')
#6 C:\PHP\Zendframework\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#7 C:\PHP\Zendframework\Zend\Controller\Front.php(212): Zend_Controller_Front->dispatch()
#8 D:\websites\maklare.easypic.se\index.php(23): Zend_Controller_Front::run('D:\websites\mak...')
#9 {main}

加载程序代码:

<?php

/**
* Action Helper for loading forms
* 
* @uses Zend_Controller_Action_Helper_Abstract
*/
class My_Helper_FormLoader extends Zend_Controller_Action_Helper_Abstract
{
/**
 * @var Zend_Loader_PluginLoader
 */
public $pluginLoader;

/**
 * Constructor: initialize plugin loader
 * 
 * @return void
 */
public function __construct()
{
    $this->pluginLoader = new Zend_Loader_PluginLoader();
}

/**
 * Load a form with the provided options
 * 
 * @param  string $name 
 * @param  array|Zend_Config $options 
 * @return Zend_Form
 */
public function loadForm($name, $options = null)
{
    $module  = $this->getRequest()->getModuleName();
    $front   = $this->getFrontController();
    $default = $front->getDispatcher()
                     ->getDefaultModule();
    if (empty($module)) {
        $module = $default;
    }
    $moduleDirectory = $front->getControllerDirectory($module);
    $formsDirectory  = dirname($moduleDirectory) . '/forms';

    $prefix = (('default' == $module) ? '' : ucfirst($module) . '_')
            . 'Form_';
    $this->pluginLoader->addPrefixPath($prefix, $formsDirectory);

    $name      = ucfirst((string) $name);
    $formClass = $this->pluginLoader->load($name);
    return new $formClass($options);
}

/**
 * Strategy pattern: call helper as broker method
 * 
 * @param  string $name 
 * @param  array|Zend_Config $options 
 * @return Zend_Form
 */
public function direct($name, $options = null)
{
    return $this->loadForm($name, $options);
}
}
?>

引导程序;

<?php

require 'appfiles\helpers\fb_sdk\src\facebook.php';

#------------------------------
# Get the paths set
#------------------------------

define('ROOT', realpath(dirname(__FILE__) . '/appfiles'));


#------------------------------
# Register the Zend autolaoder
#------------------------------

require_once ('Zend\Loader\Autoloader.php');
Zend_Loader_Autoloader::getInstance();

#------------------------------
# Dispatch
#------------------------------
require_once('appfiles\functions\functions.php');
Zend_Controller_Front::run( ROOT . '/application/controllers' );
Zend_Controller_Action_HelperBroker::addPath( ROOT .'/helpers');
?>

3 个答案:

答案 0 :(得分:2)

如果您想在添加帮助程序时保持安全,请使用绝对路径而不是相对路径,并确保正确添加帮助程序的前缀:

Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH '/Plugins/Helpers','Plugins_Helper');

或者,尝试使用AddBroker()函数添加帮助程序:

Zend_Controller_Action_HelperBroker::addPrefix('Plugins_Helper');
Zend_Controller_Action_HelperBroker::addHelper(new Plugins_Helper_FormLoader());

请注意,正确命名帮助程序类非常重要,以便zend_autoloader可以加载它们

答案 1 :(得分:0)

您可以在配置文件中配置操作帮助程序路径。

如果要在My_Helper_FormLoader

找到您的APPLICATION_PATH . '/Plugins/Helper/FormLoader.php'课程
resources.frontController.actionHelperPaths.My_Helper = APPLICATION_PATH "/Plugins/Helper"

答案 2 :(得分:0)

好的,我得出了两个结论(实际上有三个但是......)第一个是我认为Broker严重破坏,因为它没有响应代码的任何变化。它只在某些东西不起作用时默认,并且默认情况下本身不起作用。

其次,我使用了Zend_Loader_Autoloader_Resource,因为它更接近于我需要的能够拥有特定于应用程序的库。它强制执行Zend命名约定,但这没关系,因为它有效!! :)

$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
    'basePath'  => ROOT . '/helpers',
    'namespace' => 'AppFiles',
));

$resourceLoader->addResourceType('acl', 'acls/', 'Acl')
               ->addResourceType('form', 'forms/', 'Form')
               ->addResourceType('model', 'models/', 'Model');

全部谢谢!为了您的帮助和输入