About Features Downloads Getting Started Documentation Events Support GitHub

Love VuFind®? Consider becoming a financial supporter. Your support helps build a better VuFind®!

Site Tools


Warning: This page has not been updated in over over a year and may be outdated or deprecated.
development:code_generators

This is an old revision of the document!


Code Generators

Starting with VuFind 2.4, command line code generation tools are available to help save you time when performing certain common tasks.

Creating Routes

Adding new entries to the Zend Framework router can involve tedious array creation, so VuFind includes tools for creating certain kinds of routes automatically from the command line.

Static Routes

The most basic type of route simply specifies a controller and an action, mapping a URL path like “/Controller/Action” to the corresponding code.

Usage

cd $VUFIND_HOME
php public/index.php generate staticroute Controller/Action MyModule

Dynamic Routes

Simple dynamic routes are similar to static routes but allow the addition of dynamic parameters after the action name. This is useful for actions that need to, for example, retrieve specific values by ID (like the existing routes that load user favorite lists).

Usage

cd $VUFIND_HOME
php public/index.php generate dynamicroute routename Controller Action/[:placeholders] MyModule

Record Routes

Record routes are a special complex case which handle the creation of multiple route entries for a record home page, dynamic tab URLs, and a set of standard static routes. These do not commonly need to be generated, but they may be needed when creating a new search backend.

Usage

cd $VUFIND_HOME
php public/index.php generate recordroute routename-base Controller MyModule

Overriding Existing Plugins and Services

If you want to extend one of VuFind's built-in plugins or services, you can use the 'extendservice' generator to create a new subclass and automatically configure your local module to use it.

Usage

cd $VUFIND_HOME
php public/index.php generate extendservice path/to/service MyModule
  • path/to/service is a slash-separated path showing where the service is defined in one of VuFind's existing module.config.php files.
  • MyModule is the name of your local module where you wish to create a new class.

Notes

:!: Be sure that MyModule already exists and is included in your VUFIND_LOCAL_MODULES environment variable, or the generator may not work correctly. If MyModule does not yet exist, you can re-run VuFind's install.php script to create it.

:!: If things don't work right away after generating code, you may need to clear your local/cache directory to get rid of outdated configurations.

:!: Don't forget to escape backslashes in service names within the path/to/service parameter. For example, if you wanted to override the export tool, your path would be “service_manager/factories/VuFind\\Export”.

Example

Suppose you wish to create a custom subclass of VuFind's SolrMarc record driver. You can see in module/VuFind/config/module.config.php that the plugin is defined like this:

$config = array(
    'vufind' => array(
        ...
        'plugin_managers' => array(
            ...
            'recorddriver' => array(
                ...
                'factories' => array(
                    ...
                    'solrmarc' => 'VuFind\RecordDriver\Factory::getSolrMarc',

The path to this service would be vufind/plugin_managers/recorddriver/factories/solrmarc (following the nested arrays down from the top). So the command to generate a custom extension of the default class is:

cd $VUFIND_HOME
php public/index.php generate extendservice vufind/plugin_managers/recorddriver/factories/solrmarc MyModule

The output should look something like this:

Saved file: /.../module/MyModule/src/MyModule/RecordDriver/Factory.php
Saved file: /.../module/MyModule/src/MyModule/RecordDriver/SolrMarc.php
Saved file: /.../module/MyModule/src/MyModule/RecordDriver/Factory.php
Created backup: /.../module/MyModule/config/module.config.php.1423850855.bak
Successfully updated /.../module/MyModule/config/module.config.php
development/code_generators.1449866904.txt.gz · Last modified: 2015/12/11 20:48 by demiankatz