Zend Framework教程-Resources官方示例翻译

默认的引导类的基本结构如下:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
}
 

具体相应的配置文件:

; APPLICATION_PATH/configs/application.ini
[production]
autoloaderNamespaces[] = "My_"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
pluginpaths.My_Bootstrap_Resource = "My/Bootstrap/Resource"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
 
[testing : production]
[development : production]

Note:  命名空间的自动加载器
例子中使用了自定义的命名空间,我们需要通过配置文件注册配置命名空间前缀,通过autoloaderNamespaces配置选项,其是一个数组。值是具体的命名空间前缀。

例如:

autoloaderNamespaces[] = "My_"


此外,为了确保自定义插件资源能够被找到,需要注册插件前缀路径。通过pluginpaths配置选项,其是一个关联数组,key表示资源插件的类路径前缀,值是资源类的前缀的路径格式。

例如:

pluginpaths.My_Bootstrap_Resource = "My/Bootstrap/Resource"


自定义初始化是有必要的,你有两种方法。第一,你可以使用_init前缀方法;第二中方法可以定义bootstrap<resource>()方法。他们都可以接受配置数组进行相关配置。

如果资源方法指定了返回值,它会被存储在引导类的容器中。这是非查有用的功能,不同的资源需要进行交互(例如一个资源注入到另一个中)时,可以用getResource() 方法来获取这些值。

下面的例子展示了一个初始化请求对象的资源方法。可以使用依赖性跟踪(它依赖前端控制器资源),获取引导资源,并返回存储在引导类的值。

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initRequest()
    {
        // Ensure front controller instance is present, and fetch it
        $this->bootstrap('FrontController');
        $front = $this->getResource('FrontController');
 
        // Initialize the request object
        $request = new Zend_Controller_Request_Http();
        $request->setBaseUrl('/foo');
 
        // Add it to the front controller
        $front->setRequest($request);
 
        // Bootstrap will store this value in the 'request' key of its container
        return $request;
    }
}

请注意,在这个例子中,需要调用bootstrap();来确保调用此方法之前前端控制器已经初始化。 调用会触发另一个资源或者另一个类中的方法。

另一种选择是使用资源插件。资源插件是执行特定的初始化的对象,具体规定如下:

Zend_Application 对象的实例化时

在引导对象初始化的时候  

明确地通过方法调用引导对象时

资源插件实现了Zend_Application_Resource_ResourceAbstract,Zend_Application_Resource_ResourceAbstract简单的定义了,注入引导对象和配置选项,以及 init()方法。例如,一个自定义“View”:

class My_Bootstrap_Resource_View
    extends Zend_Application_Resource_ResourceAbstract
{
    public function init()
    {
        $view = new Zend_View($this->getOptions());
        Zend_Dojo::enableView($view);
 
        $view->doctype('XHTML1_STRICT');
        $view->headTitle()->setSeparator(' - ')->append('My Site');
        $view->headMeta()->appendHttpEquiv('Content-Type',
                                           'text/html; charset=utf-8');
 
        $view->dojo()->setDjConfigOption('parseOnLoad', true)
                     ->setLocalPath('/js/dojo/dojo.js')
                     ->registerModulePath('../spindle', 'spindle')
                     ->addStylesheetModule('spindle.themes.spindle')
                     ->requireModule('spindle.main')
                     ->disable();
 
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
            'ViewRenderer'
        );
        $viewRenderer->setView($view);
 
        return $view;
    }
}


你可以提供资源插件类的名称,或一个插件加载器前缀路径的组合和资源插件的短名称(例如,“view”)使用资源插件:

$application = new Zend_Application(
    APPLICATION_ENV,
    array(
        'resources' => array(
            'My_Bootstrap_Resource_View' => array(), // full class name; OR
            'view' => array(),                       // short name
 
            'FrontController' => array(
                'controllerDirectory' => APPLICATION_PATH . '/controllers',
            ),
        ),
 
        // For short names, define plugin paths:
        'pluginPaths = array(
            'My_Bootstrap_Resource' => 'My/Bootstrap/Resource',
        )
    )
);

通过访问父引导类,资源插件可以调用其他资源和初始化函数:

class My_Bootstrap_Resource_Layout
    extends Zend_Application_Resource_ResourceAbstract
{
    public function init()
    {
        // ensure view is initialized...
        $this->getBootstrap()->bootstrap('view');
 
        // Get view object:
        $view = $this->getBootstrap()->getResource('view');
 
        // ...
    }
}

在正常使用中,您可以实例化应用程序,引导,并运行它:

$application = new Zend_Application(...);
$application->bootstrap()
            ->run();


通常,您可能需要初始化特定的资源:

$application = new Zend_Application(...);
$application->getBootstrap()->bootstrap('db');
 
$service = new Zend_XmlRpc_Server();
$service->setClass('Foo');  // uses database...
echo $service->handle();


 你可以重载 bootstrap() 调用内部方法或资源:



$application = new Zend_Application(...);
$application->getBootstrap()->bootstrapDb();



相关原文网址

http://framework.zend.com/manual/en/zend.application.html
http://framework.zend.com/manual/en/zend.application.examples.html
http://framework.zend.com/manual/en/zend.application.core-functionality.html
http://framework.zend.com/manual/en/zend.application.available-resources.html
http://aaronsaray.com/blog/2011/05/24/zend-framework-bootstrap-vs-front-controller-plugin/
http://blog.straylightrun.net/2011/04/13/zend-framework-bootstrap-php-or-they-hung-him-up-by-his-bootstraps/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值