tomphp/tjo-annotation-router

This package is abandoned and no longer maintained. No replacement package was suggested.

ZF2 module which lets routing be configured by via annotations on your controller classes..

dev-master 2013-08-10 16:10 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:23:53 UTC


README

Build Status

This module allows the use of annotations in your controller classes to configure the router.

Installation

Added the following requirement to your projects composer.json file.

"tomphp/tjo-annotation-router": "dev-master"

and run

php ./composer.phar update

and finally add TjoAnnotationRouter to your modules list in config/application.php.

Usage

Annotating the controller

First up annotate your controller like so:

<?php

namespace DemoModule\Controller;

use TjoAnnotationRouter\Annotation as Router;

/**
 * @Router\Base("demo")
 */
class TestController extends AbstractActionController
{
    /**
     * @Router\Route(type="literal", name="index", route="/index")
     */
    public function indexAction()
    {
        // Action stuff here
    }

    /**
     * @Router\Route(type="segment", name="another-page", route="/page1/:id")
     * @Router\Constraint(param="id", rule="[0-9]+")
     * @Router\DefaultValue(param="id", value="7")
     */
    public function anotherPageAction()
    {
        // More action stuff here
    }
}

The Annotations

  • @Router\Base - Sets the route that all annotated routes fall under. You can use / to specify multiple levels.
  • @Router\Route - Sets the route for this action.
  • @Router\DefaultValue - Set a default value for a parameter, use one of these annotations for each param you wish to specify a value for.
  • @Router\Constraint - Set a constraint for a parameter, again use mulitple annotations for multiple parameters.

Configuring the module

Currently the only configuration option available is the path to the cache file. If you wish to update this simply add this to your project/module config:

    'tjo_annotation_router' => array(
        'cache_file' => 'path/to/cache/file.php',
    ),

Caching

Parsing the route annotations every request will slow down your application quite significantly. To combat this a caching solution is provided. To build the cache simple run the following command from the command line:

vendor/bin/cache_routes.php

If you wish, you modify any annotations after building the cache simply run this command again.

If you want to turn off to caching at any time just remove data/TjoAnnotation/routes.php.

Issues with other modules

BjyAuthorize

This module uses ZF2's controller manager to fetch the names of the controllers, the manager returns the names a lowercase alpha strings with all other characters stripped out. BjyAuthorize is more strict about the controller names in it's guard settings so you have to put the ZF version in the BjyAuthorize guard config rather than the way you might prefer.

e.g.

Instead of using Application\Controller\MyController you have to use applicationcontrollermycontroller.

TODO List

This is currently a very early version of the module. Jobs on my TODO list are:

  • Build a test suite
  • Refactor
  • Bug fix
  • Improve functionality

Please comment, suggest, fork, etc.