2.4. Resources

2.4. Resources

Resources are the classes that bootstrap each component that an application may need. They are the building blocks of bootstrapping as they specialize in configuring and setting up only a specific items. Each resource may or may not have configuration options.

2.4.1. Resources included in Zym

Zym includes basic resources required to bootstrap a project.

[Note] Note about config file examples

Config file examples are provided in XML form and lack the general config environment skeleton to reduce redundancy. See ??? for more info.

2.4.1.1. Autoload Resource

Autoloading simplifies development and dependency concerns. This resource handles the setting up of autoloading systems such as Zend_Loader. Loading this resource by default is the same as calling Zend_Loader::registerAutoload().

Table 2.2. Autoload Resource Options

Key Default Description
class
default Zend_Loader
Class to use as autoloader. It should implement Zym_Loader_Autoload_Interface or have an autoload() function. This key can be specified multiple times in xml to create an key => array data structure which will register all autoloaders.

[Note] Note

Priority is high

2.4.1.2. Db Resource

This resource sets up Zend_Db and can support single or multi-db connections. The default_config element is used to supply defaults for all db connections. It is also a template for a single db connection config. Each connection config supports config elements from Zend_Db::factory(). See the Zend_Db factory usage section with Zend_Config for more info.

[Note] Config Inheritance

Because this resource supports handling of multiple connections, each connection config inherits from the "default_config" element instead of from its name like in other resources.

Example 2.7. Single-DB Connection Config Example

In this example, the standard options from Zend_Db::factory() are set and the registry key to set the adapter instance to is set to "Db-1". From the application you can retrieve the object via $db = Zend_Registry::get('Db-1'). This connection inherits all configs from the "default_config" element.

<?xml version="1.0" encoding="UTF-8"?>
<adapter>PDO_MYSQL</adapter>
<dbname>my_app</dbname>
<host>10.10.10.2</host>
<username>foo</username>
<password>bar</password>
<registry>
    <key>Db-1</key>
</registry>

Example 2.8. Multi-DB Connection Config Example

<?xml version="1.0" encoding="UTF-8"?>
<connection>
    <site>
        <adapter>PDO_MYSQL</adapter>
        <dbname>app_site</dbname>
    </site>
    <forum>
        <adapter>Mysqli</adapter>
        <dbname>app_forum</dbname>
        <registry>
            <key>db-forum</key>
        </registry>
        <set_default_adapter>
            <adapter>Forum_Db_Table_Abstract</adapter>
        </set_default_adapter>
    </forum>
</connection>

Table 2.3. Db Resource Options

Key Default Description
default_config.adapter
default Mysqli
Default DB adapter
default_config.registry.disabled
default false
Disable setting of db adapter to the registry
default_config.registry.key
default db
Registry key to set adapter to.
default_config.set_default_adapter
default Zend_Db_Table_Abstract
Set adapter to Zend_Db_Table_Abstract::setDefaultAdapter().
default_config.profiler.enabled
default false
development true
Enabled the DB profiler
default_config.profiler.enabled
default false
development true
Enabled the DB profiler
default_config.profiler.class
default  
Profiler class
default_config.profiler.filter.elapsed_secs
default  
Elapsed seconds filter (setFilterElapsedSecs())
default_config.profiler.filter.query_type
default  
Elapsed seconds filter (setFilterQueryType())
connection
default array()
Array of db connection configs in key/value form. Key being an identifier and value being the

2.4.1.3. Cache Resource

This resource configures the Zym_Cache factory which enables application wide cache control.

This resource's configuration options are 1:1 with Zym_Cache::setConfig() except for the cavet that 'cache_dir' and 'cache_db_complete_path' paths are relative to Zym_App::PATH_DATA. Below are the table of options that are overridden by default based on the environment.

Table 2.4. Cache Resource Options

Key Default Description
default_backend file
frontend  
frontend.core.automatic_serialization true

[Note] Note

Priority is high

2.4.1.4. Controller Resource

This resource sets up Zend_Controller and its related components.

Table 2.5. Controller Resource Options

Key Default Description
class
default Zend_Controller_Front
Zend_Controller_Front class to setup
throw_exception
default false
development true
Whether Zend_Controller_Front should throw exceptions or catch them. (throwExceptions())
module.directory
default array('modules/')
Array of path to directories containing modules (addModuleDirectory()). Paths are relative to the application directory.
module.controller_name
default  
Set the module controller directory name (setModuleControllerDirectoryName())
controller.directory
default array()
Add controller directories (addControllerDirectory). Accepts an array in key/value pairs. Keys are used as module names and values as paths relative to the application directory
base_url
default  
Set the baseUrl (setBaseUrl())
plugin
default array()
Array of plugins to load. Plugins class should be specified as keys. The value is an array containing 'plugin_index' to specify a plugin loading index. If the plugin class implements Zym_App_Resource_Controller_Plugin_Interface, the array value can contain config. registerPlugin()
plugin.{plugin name}.plugin_index
default  
An integer representing the plugin loading index for Zend_Controller_Front
helper_broker.paths
default array()
Add helper broker paths. Paths should be added as an array containing the 'path' and the 'prefix' key. The key name does not matter as it is only used as an identifier/index only.
default.action
default  
Set default action name (setDefaultAction())
default.controller_name
default  
Set default controller name (setDefaultControllerName())
default.module
default  
Set default module name(setDefaultModule())
params
default array()
Array of key/value params to set to the Front Controller (setParams()).
request
default Zym_Controller_Request_Http
Set request class (setRequest()).
response
default Zym_Controller_Response_Http
Set response class (setResponse()).
dispatcher
default  
Set dispatcher class (setDispatcher()).
router
default  
Set router class (setRouter()).

2.4.1.5. Json Resource

This resource sets up Zend_Json

Table 2.6. Json Resource Options

Key Default Description
class
default Zend_Json
Zend_Json class to setup
use_builtin_encoder_decoder
default null
Zend_Json::$use_builtin_encoder_decoder = (true|false)
max_recursion_depth_allowed
default null
Zend_Json::$max_recursion_depth_allowed = (int)

2.4.1.6. Layout Resource

This resource sets up Zend_Layout

Table 2.7. Layout Resource Options

Key Default Description
layout_path
default layouts
Path to layouts directory relative to the application directory (app/layouts)
layout
default default
Default layout name (default.phtml)
content_key
default  
mvc_enabled
default  

2.4.1.7. Locale Resource

This resource sets up Zend_Locale

Table 2.8. Locale Resource Options

Key Default Description
cache
default false
production true
Enables caching using Zym_Cache. Zend_Locale::setCache()
class
default Zend_Locale
Locale class to use
default null Default locale to fall back when auto detection fails: Zend_Locale::setDefault()
locale null Default site locale to use when setting to the registry
registry.enabled true Set locale to registry
registry.key Zend_Locale Registry key

[Note] Note

Priority is high

2.4.1.8. Mail Resource

This resource sets up Zend_Mail with default transports.

Table 2.9. Mail Resource Options

Key Default Description
default_transport
default  
Accepts a Zend_Mail transport object or a transport type. ex1. array('default_transport' => new Zend_Mail_Transport_Sendmail()) ex2. 'sendmail' ex3. 'smtp'
transport
default array()
Array for the config of transports. The key should be the name of the transport and the value should be an array of the config values. array('smtp' => array('host' => 'smtp.gmail.com'))
transport_map
default array()
Map used for custom transport config handlers. Accepts an array with the key as the transport name and the map specified as prefix/path keys or a single namespace prefix. array('sendmail' => 'My_App_Resource_Mail_Transport') array('sendmail' => array('prefix' => 'My_App_Resource_Mail_Transport' 'path' => 'My/App/Resource/Mail/Transport'))

2.4.1.9. Navigation Resource

This resource sets up Zym_Navigation

Example 2.9. Setting up the navigation resource

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
Navigation Resource Configuration
    This resource is used to setup the Zym_Navigation
 
================================================================================
Tags below <resource> are configuration for each environment set in Zym_App.
 
Each section below extends the <default> config section if it has the 
"extends" attribute. Adding a tag in <production> would override the value
set from <default> for the production environment.
-->
<resource>
    <!--
    Production environment overrides
    -->
    <production extends="default"></production>
 
    <!--
    Test environment overrides
    -->
    <test extends="default"></test>
 
    <!--
    Development environment overrides
    -->
    <development extends="default"></development>
 
    <!--
    Default environment config
 
    This environment is the default for all environments that extend it.
 
    A key specified here will override both the defaults and environment config
    set inside the resource.
    -->
    <default>
        <pages>
            <home>
                <label>Home</label>
                <action>index</action>
                <controller>index</controller>
            </home>
            <demoHome>
                <label>Playground</label>
                <action>index</action>
                <controller>index</controller>
                <module>demo</module>
                <pages>
                    <contextSwitch>
                        <label>ContextSwitch</label>
                        <action>index</action>
                        <controller>context-switch</controller>
                        <module>demo</module>
                    </contextSwitch>
                    <error>
                        <label>Error</label>
                        <action>index</action>
                        <controller>error</controller>
                        <module>demo</module>
                    </error>
                    <flashMessenger>
                        <label>Flash Messenger</label>
                        <action>index</action>
                        <controller>flash-messenger</controller>
                        <module>demo</module>
                    </flashMessenger>
                    <navigation>
                        <label>Navigation</label>
                        <action>index</action>
                        <controller>navigation</controller>
                        <module>demo</module>
                    </navigation>
                    <notification>
                        <label>Notification</label>
                        <action>index</action>
                        <controller>notification</controller>
                        <module>demo</module>
                    </notification>
                    <pagination>
                        <label>Pagination</label>
                        <action>index</action>
                        <controller>paginate</controller>
                        <module>demo</module>
                    </pagination>
                    <translation>
                        <label>Translation</label>
                        <action>index</action>
                        <controller>translate</controller>
                        <module>demo</module>
                    </translation>
                </pages>
            </demoHome>
        </pages>
    </default>
</resource>

Table 2.10. Navigation Resource Options

Key Default Description
registry_key
default Zym_Navigation
Registry to set the object to. (Zend_Registry::set('Zym_Navigation', $nav))
pages
default array()
Pages config

2.4.1.10. PHP Resource

This resource sets up PHP ini_set() options. All available ini_set() options are separated by "." The table below shows the defaults that loading this resource will set.

"date.timezone"
// XML
<date>
    <timezone>UTC</timezone>
</date>
 
// INI
date.timezone = UTC
 
// Array()
'date' => array(
    'timezone' => 'UTC'
)

Table 2.11. PHP Resource Options

Key Default Description
display_errors
default false
development true
Whether to display errors or not
date.force_default_timezone
default false
Custom resource setting that allows you to force the default server timezone. This prevents the set default timezone warnings from PHP.
date.timezone
default Europe/London
Default timezone
error_reporting
default 341
development 8191
Error reporting setting... 341 = E_PARSE | E_COMPILE_ERROR | E_ERROR | E_CORE_ERROR | E_USER_ERROR 8191 = E_ALL | E_STRICT
include_path
default  
Accepts an array or an include_path string. Path separators (':', ';') do not matter as they are automatically translated according to the OS by the resource.

2.4.1.11. Route Resource

This resource adds routes to the router in Zend_Controller_Front. It is exactly the same as Zend_Controller_Front::getInstance()->getRouter()->addConfig(). Config options are those for the router

Example 2.10. Route Resource config

<user>
    <type>Zym_Controller_Router_Route_HttpHost</type>
    <host>:user.*.*</host>
    <route>:action/*</route>
    <defaults>
        <module>demo</module>
        <controller>form</controller>
        <action>index</action>
    </defaults>
    <reqs>
        <!-- Since its a subdomain, prevent matching www -->
        <user>&amp;lt;![CDATA[^.+(?<!^(www))$]]&amp;gt;</user>
    </reqs>
</user>

2.4.1.12. Session Resource

This resource sets up Zend_Session

Table 2.12. Session Resource Options

Key Default Description
auto_start
default true
If true, it will call Zend_Session::start()
config
default array()
This is the array used to populate Zend_Session::setOptions()
config.save_path
default session/
This is the session save path relative to Zym_App's data directory. Path can be absolute.
config.name
default %s_SID
The session name which should be unique. Accepts "%s" as a placeholder for the application's unique name specified by Zym_App::getName(true). eg. "MyProject_SID"
save_handler
default array()
Array containing options for setting up session save handler.
save_handler.class_name
default null/
Class name to use as session save handler. An instance of this class will be constructed and passed to Zend_Session::setSaveHandler(), e.g. Zend_Session_SaveHandler_DbTable.
save_handler.constructor_args
default array()
An array of arguments to pass to the save handler constructor. Each key in the array will be added as an a constructor takes an array of options, the constructor_args array should contain one element, which should also be an array.

2.4.1.13. Translate Resource

This resource sets up Zend_Translate

Example 2.11. Translate resource gettext usage example

<adapter>gettext</adapter>
<options>
    <scan>directory</scan>
</options>

Table 2.13. Translate Resource Options

Key Default Description
adapter
default tmx
Zend_Translate adapter to use.
cache
default false
production true
Setup caching with Zend_Translate::setCache(). Uses Zym_Cache for retrieving the cache object.
data
default locale/
Path to locale data relative to the data directory value of Zym_App
options
default  
Options in array form
registry.enabled
default true
Whether to set the translate object to the registry (Zend_Registry).
registry.key
default Zend_Translate
Name of the registry key to set the translate object to.

2.4.1.14. View Resource

This resource sets up Zend_View and the view renderer (Zend_Controller_Action_Helper_ViewRenderer)

Table 2.14. View Resource Options

Key Default Description
view.class
default Zym_View
A view class that implements Zend_View_Interface
view.encoding
default  
Set view encoding method (setEncoding)
view.escape
default  
Set view escaping method (setEscape()).
view.filter
default array()
Array of filters to add for script filtering (addFilter()).
view.helper
default array()
Array of helpers to instantiate> this is useful for stuff like setting doctype. The key is the helper name and the value is an array used as constructor params via call_user_func_array();
view.helper.doctype
default XHTML1_STRICT
The default doctype is set to xhtml strict.
view.path.base
default array()
Array of base paths to add (addBasePath()). Keys are not used, but serve as a helpful identifier. Each array should contain 'path' and 'prefix' if needed.
view.path.filter
default array()
Array of filter paths to add (addFilterPath()). Keys are not used, but serve as a helpful identifier. Each array should contain 'path' and 'prefix' if needed.
view.path.helper
default array()
Array of helper paths to add (addHelperPath()). Keys are not used, but serve as a helpful identifier. Each array should contain 'path' and 'prefix' if needed.
view.path.script
default array()
Array of script paths to add (addScriptPath()). Format is key/value with value as the path. Keys are not used, but serve as a helpful identifier.
view.stream.flag
default  
Set stream flag which determines if streams should be used (setStreamFlag).
view.stream.protocol
default  
Set stream protocol name (setStreamProtocol).
view.stream.wrapper
default  
Set stream wrapper class (setStreamWrapper).
view.stream.filter
default  
Add stream filter (addStreamFilter()).
view_renderer.suffix
default  
Set view script suffix (setViewSuffix()).
view_renderer.spec.base_path
default  
Set base path spec (setViewBasePathSpec()).
view_renderer.spec.script_path
default  
Set script path spec (setViewScriptPathSpec()).
view_renderer.spec.script_path_no_controller
default  
Set script path no controller spec (setViewScriptPathNoControllerSpec()).
view_renderer.never_controller
default  
Set never controller flag (setNeverController()).
view_renderer.never_render
default  
Set never render flag (setNeverRender()).
view_renderer.no_controller
default  
Set no controller flag (setNoController()).
view_renderer.no_render
default  
Set no render flag (setNoRender()).

2.4.2. Resource dispatching priorities

The order that resources are specified is FIFO; however, resources can have priority overrides which ensures that a resource needed earlier in the bootstrapping process is dispatched. Priority levels are specified as integers where the lower the number, the higher the priority. Default constants provided in Zym_App_Resource_Abstract are PRIORITY_HIGH, PRIORITY_MEDIUM, and PRIORITY_LOW.

  • Zym_App_Resource_Abstract::PRIORITY_HIGH = 10

  • Zym_App_Resource_Abstract::PRIORITY_MEDIUM = 25

  • Zym_App_Resource_Abstract::PRIORITY_LOW = 50

[Note] Note

A negative value is possible.

2.4.3. Caching in resources

Resources can take advantage of caching if Zym_App is configured to cache. In addition to resource handled caching where appropriate, caching of the resource config object is handled in Zym_App_Resource_Abstract.