tomphp/tjo-annotation-router

此包已被废弃,不再维护。未建议替代包。

一个ZF2模块,允许通过控制器类上的注释配置路由。

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

此模块允许您使用控制器类中的注释来配置路由。

安装

将以下要求添加到您的项目的composer.json文件中。

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

然后运行

php ./composer.phar update

最后将 TjoAnnotationRouter 添加到 config/application.php 中的模块列表中。

用法

注释控制器

首先按照如下方式注释您的控制器

<?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
    }
}

注释

  • @Router\Base - 设置所有注释路由所属的路由。您可以使用 / 来指定多个级别。
  • @Router\Route - 设置此操作的路径。
  • @Router\DefaultValue - 为参数设置默认值,对于每个希望指定值的参数,使用这些注释之一。
  • @Router\Constraint - 为参数设置约束,再次使用多个注释来指定多个参数。

配置模块

目前唯一的配置选项是缓存文件的路径。如果您想更新此路径,只需将其添加到项目/模块配置中

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

缓存

每次请求解析路由注释都会显著降低您的应用程序速度。为了解决这个问题,提供了一个缓存解决方案。要构建缓存,只需从命令行运行以下命令

vendor/bin/cache_routes.php

如果您想在构建缓存后修改任何注释,只需再次运行此命令。

如果您想在任何时候关闭缓存,只需删除 data/TjoAnnotation/routes.php

与其他模块的问题

BjyAuthorize

此模块使用ZF2的控制器管理器来获取控制器名称,管理器返回的名称是删除所有其他字符的小写字母字符串。BjyAuthorize对其守卫设置中的控制器名称要求更严格,因此您必须在BjyAuthorize守卫配置中使用ZF版本,而不是您可能更喜欢的设置。

例如

而不是使用 Application\Controller\MyController,您必须使用 applicationcontrollermycontroller

待办事项列表

此模块目前是一个非常早期的版本。我待办事项列表中的工作包括

  • 构建测试套件
  • 重构
  • 修复错误
  • 改进功能

请评论、建议、分支等。