4.6. Available Resource Plugins
2010-01-12 00:00:01   来源:   评论:0 点击:

Here you'll find API-like documentation about all resource plugins available by default in Zend_Application. 4.6.1. Zend_Application_Resource_Db Zend_Applicati...

Here you'll find API-like documentation about all resource plugins available by default in Zend_Application.

4.6.1. Zend_Application_Resource_Db

Zend_Application_Resource_Db will initialize a Zend_Db adapter based on the options passed to it. By default, it also sets the adapter as the default adapter for use with Zend_Db_Table.

The following configuration keys are recognized:

  • adapter: Zend_Db adapter type.

  • params: associative array of configuration parameters to use when retrieving the adapter instance.

  • isDefaultTableAdapter: whether or not to establish this adapter as the default table adapter.

例 4.1. Sample DB adapter resource configuration

Below is an example INI configuration that can be used to initialize the DB resource.


[production]
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "webuser"
resources.db.params.password = "XXXXXXX"
resources.db.params.dbname = "test"
resources.db.isDefaultTableAdapter = true

[注意] Retrieving the Adapter instance

If you choose not to make the adapter instantiated with this resource the default table adapter, how do you retrieve the adapter instance

As with any resource plugin, you can fetch the DB resource plugin from your bootstrap:


$resource = $bootstrap->getPluginResource('db');

Once you have the resource object, you can fetch the DB adapter using the getDbAdapter() method:


$db = $resource->getDbAdapter();

4.6.2. Zend_Application_Resource_Frontcontroller

Probably the most common resource you will load with Zend_Application will be the Front Controller resource, which provides the ability to configure Zend_Controller_Front. This resource provides the ability to set arbitrary front controller parameters, specify plugins to initialize, and much more.

Once initialized, the resource assigns the $frontController property of the bootstrap to the Zend_Controller_Front instance.

Available configuration keys include the following, and are case insensitive:

  • controllerDirectory: either a string value specifying a single controller directory, or an array of module to controller directory pairs.

  • moduleControllerDirectoryName: a string value indicating the subdirectory under a module that contains controllers.

  • moduleDirectory: directory under which modules may be found.

  • defaultControllerName: base name of the default controller (normally "index").

  • defaultAction: base name of the default action (normally "index").

  • defaultModule: base name of the default module (normally "default").

  • baseUrl: explicit base URL to the application (normally auto-detected).

  • plugins: array of front controller plugin class names. The resource will instantiate each class (with no constructor arguments) and then register the instance with the front controller.

  • params: array of key to value pairs to register with the front controller.

If an unrecognized key is provided, it is registered as a front controller parameter by passing it to setParam().

例 4.2. Sample Front Controller resource configuration

Below is a sample INI snippet showing how to configure the front controller resource.


[production]
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleControllerDirectoryName = "actions"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.defaultControllerName = "site"
resources.frontController.defaultAction = "home"
resources.frontController.defaultModule = "static"
resources.frontController.baseUrl = "/subdir"
resources.frontController.plugins.foo = "My_Plugin_Foo"
resources.frontController.plugins.bar = "My_Plugin_Bar"
resources.frontController.env = APPLICATION_ENV

例 4.3. Retrieving the Front Controller in your bootstrap

Once your Front Controller resource has been initialized, you can fetch the Front Controller instance via the $frontController property of your bootstrap.


$bootstrap->bootstrap('frontController');
$front = $bootstrap->frontController;

4.6.3. Zend_Application_Resource_Modules

Zend_Application_Resource_Modules is used to initialize your application modules. If your module has a Bootstrap.php file in its root, and it contains a class named Module_Bootstrap (where "Module" is the module name), then it will use that class to bootstrap the module.

By default, an instance of Zend_Application_Module_Autoloader will be created for the module, using the module name and directory to initialize it.

Since the Modules resource does not take any arguments by default, in order to enable it via configuration, you need to create it as an empty array. In INI style configuration, this looks like:


resources.modules[] =

In XML style configuration, this looks like:


<resources>
    <modules>
        <!-- Placeholder to ensure an array is created
        <placeholder />
    </modules>
</resources>

Using a standard PHP array, simply create it as an empty array:


$options = array(
    'resources' => array(
        'modules' => array(),
    ),
);
[注意] Front Controller Resource Dependency

The Modules resource has a dependency on the Front Controller resource. You can, of course, provide your own replacement for that resource via a custom Front Controller resource class or a class initializer method -- so long as the resource plugin class ends in "Frontcontroller" or the initializer method is named "_initFrontController" (case insensitive).

例 4.4. Configuring Modules

You can specify module-specific configuration using the module name as a prefix or sub-section in your configuration file.

For example, let's assume that your application has a "news" module. The following are INI and XML examples showing configuration of resources in that module.


[production]
news.resources.db.adapter = "pdo_mysql"
news.resources.db.params.host = "localhost"
news.resources.db.params.username = "webuser"
news.resources.db.params.password = "XXXXXXX"
news.resources.db.params.dbname = "news"

< xml version="1.0" >
<config>
    <production>
        <news>
            <resources>
                <db>
                    <adapter>pdo_mysql</adapter>
                    <params>
                        <host>localhost</host>
                        <username>webuser</username>
                        <password>XXXXXXX</password>
                        <dbname>news</dbname>
                    </params>
                    <isDefaultAdapter>true</isDefaultAdapter>
                </db>
            </resources>
        </news>
    </production>
</config>

例 4.5. Retrieving a specific module bootstrap

On occasion, you may need to retrieve the bootstrap object for a specific module -- perhaps to run discrete bootstrap methods, or to fetch the autoloader in order to configure it. This can be done using the Modules resource's getExecutedBootstraps() method.


$resource = $bootstrap->getPluginResource('modules');
$moduleBootstraps = $resource->getExecutedBootstraps();
$newsBootstrap = $moduleBootstraps['news'];

4.6.4. Zend_Application_Resource_Navigation

Zend_Application_Resource_Navigation can be used to configure a Zend_Navigation instance. Configuration options are per the Zend_Navigation options.

Once done configuring the navigation instance, it by assigns the instance to Zend_View_Helper_Navigation by default -- from which you may retrieve it later.

例 4.6. Sample Navigation resource configuration

Below is a sample INI snippet showing how to configure the navigation resource.


resources.navigation.pages.page1.label = "Label of the first page"
resources.navigation.pages.page1.route = "Route that belongs to the first page"

; Page 2 is a subpage of page 1
resources.navigation.pages.page1.pages.page2.type = "Zend_Navigation_Page_Uri"
resources.navigation.pages.page1.pages.page2.label = "Label of the second page"
resources.navigation.pages.page1.pages.page2.uri = "/url/to/page/2"

4.6.5. Zend_Application_Resource_Session

Zend_Application_Resource_Session allows you to configure Zend_Session as well as optionally initialize a session SaveHandler.

To set a session save handler, simply pass the saveHandler (case insensitive) option key to the resource. The value of this option may be one of the following:

  • String: A string indicating a class implementing Zend_Session_SaveHandler_Interface that should be instantiated.

  • Array: An array with the keys "class" and, optionally, "options", indicating a class implementing Zend_Session_SaveHandler_Interface that should be instantiated and an array of options to provide to its constructor.

  • Zend_Session_SaveHandler_Interface: an object implementing this interface.

Any other option keys passed will be passed to Zend_Session::setOptions() to configure Zend_Session.

例 4.7. Sample Session resource configuration

Below is a sample INI snippet showing how to configure the session resource. It sets several Zend_Session options, as well as configures a Zend_Session_SaveHandler_DbTable instance.


resources.session.save_path = APPLICATION_PATH "/../data/session"
resources.session.use_only_cookies = true
resources.session.remember_me_seconds = 864000
resources.session.saveHandler.class = "Zend_Session_SaveHandler_DbTable"
resources.session.saveHandler.options.name = "session"
resources.session.saveHandler.options.primary.session_id = "session_id"
resources.session.saveHandler.options.primary.save_path = "save_path"
resources.session.saveHandler.options.primary.name = "name"
resources.session.saveHandler.options.primaryAssignment.sessionId = "sessionId"
resources.session.saveHandler.options.primaryAssignment.sessionSavePath = "sessionSavePath"
resources.session.saveHandler.options.primaryAssignment.sessionName = "sessionName"
resources.session.saveHandler.options.modifiedColumn = "modified"
resources.session.saveHandler.options.dataColumn = "session_data"
resources.session.saveHandler.options.lifetimeColumn = "lifetime"

[注意] Bootstrap your database first!

If you are configuring the Zend_Session_SaveHandler_DbTable session save handler, you must first configure your database connection for it to work. Do this by either using the Db resource -- and make sure the "resources.db" key comes prior to the "resources.session" key -- or by writing your own resource that initializes the database, and specifically sets the default Zend_Db_Table adapter.

4.6.6. Zend_Application_Resource_View

Zend_Application_Resource_View can be used to configure a Zend_View instance. Configuration options are per the Zend_View options.

Once done configuring the view instance, it creates an instance of Zend_Controller_Action_Helper_ViewRenderer and registers the ViewRenderer with Zend_Controller_Action_HelperBroker -- from which you may retrieve it later.

例 4.8. Sample View resource configuration

Below is a sample INI snippet showing how to configure the view resource.


resources.view.encoding = "UTF-8"
resources.view.basePath = APPLICATION_PATH "/views/scripts"

相关热词搜索:  resource default your

上一篇:第 5 章 Zend_Auth
下一篇:4.5. Core Functionality

分享到: 收藏
频道总排行
频道本月排行