1

在 Zend 框架中,如何在 bootstrap.php 文件中使用setHelperPath方法使框架可以访问“My_View_Helper_Test”(假设 Helper 绝对路径是常量 'MY_PATH')?

我的 index.php

//identify the location of th application dir in respect to 
//the botstrap file's location, and configure PHP's include_path to
//include the library directory's location

define('APPLICATION_PATH',realpath(dirname(__FILE__).'/../application'));
set_include_path(APPLICATION_PATH.'/../library'.PATH_SEPARATOR.get_include_path());


//give the zend framework the ability to load classes on demand,
//as you request them,rather than having to deal with require() statements.

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

//retrieve the BOOTSTRAP file
try
{
require'../application/bootstrap.php';  
}
catch(Exception $exception)
{
printf('Could not locate bootstrap.php');
exit(1);    
}

//start using the front controller in order to route all requests
Zend_Controller_Front::getInstance()->dispatch();

我的 bootstrap.php

//configure the site environment status

defined('APPLICATION_ENVIRONMENT')or define('APPLICATION_ENVIRONMENT','development');

//invoke the front controller
$frontController=Zend_Controller_Front::getInstance();

//identify the location of the controller directory
$frontController->setControllerDirectory(APPLICATION_PATH.'/controllers');

//create the env parameter so you can later access the environment
//status within the application

$frontController->setParam('env',APPLICATION_ENVIRONMENT);

//clean up all allocated script resources
unset($frontController);

谢谢

卢卡

4

1 回答 1

2

首先你应该有一个像这样的引导类:http: //framework.zend.com/manual/en/zend.application.examples.html

然后在您的引导类中,您将添加一个方法来初始化视图并添加视图助手路径:

    /**
 * Initializes the view
 *
 * @return Zend_View A view object
 */
protected function _initView()
{
    // Initialize view
    $view = new Zend_View();

    // Add view helper path
    $view->addHelperPath(
        MY_PATH,
        'My_View_Helper'
    );

    // Add the view to the ViewRenderer
    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
        'ViewRenderer'
    );
    $viewRenderer->setView($view);

    // Return it, so that it can be stored by the bootstrap
    return $view;
}
于 2011-05-11T18:11:50.563 回答