saxulum/saxulum-route-controller-provider

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

Saxulum 路由/控制器提供者

1.3.0 2015-12-13 12:27 UTC

This package is not auto-updated.

Last update: 2020-09-22 19:07:57 UTC


README

与 plain silex-php 兼容

Build Status Total Downloads Latest Stable Version Scrutinizer Code Quality

功能

  • 使用注解将控制器注册为服务

需求

  • php >=5.3
  • Doctrine Annotations >=1.1
  • PHP Parser >=0.9,<1.0
  • Saxulum ClassFinder >=1.0
  • Symfony Finder Component >=2.3
  • Silex >= 1.1

安装

通过 Composer 作为 saxulum/saxulum-route-controller-provider 安装。

AnnotationRegistry

在添加了 composer 的 autoload.php 之后添加此行

\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(
    array($loader, 'loadClass')
);

带有定义的缓存目录

use Saxulum\RouteController\Provider\RouteControllerProvider;
use Silex\Provider\ServiceControllerServiceProvider;

$app->register(new ServiceControllerServiceProvider());
$app->register(new RouteControllerProvider(), array(
    'route_controller_cache' => '/path/to/cache/'
));
  • debug == true: 每次加载时都会构建缓存文件
  • debug == false: 如果不存在,则会构建缓存文件;如果其不同步,则会删除它

没有定义的缓存目录

可能更慢,因为需要清理临时目录

use Saxulum\RouteController\Provider\RouteControllerProvider;
use Silex\Provider\ServiceControllerServiceProvider;

$app->register(new ServiceControllerServiceProvider());
$app->register(new RouteControllerProvider());
  • debug == true: 每次加载时都会构建缓存文件
  • debug == false: 如果不存在,则会构建缓存文件;如果其不同步,则会删除它

添加控制器路径

$app['route_controller_paths'] = $app->share(
    $app->extend('route_controller_paths', function ($paths) {
        $paths[] = '/path/to/the/controllers';

        return $paths;
    })
);

使用方法

路由注解

控制器

use Saxulum\RouteController\Annotation\Route;

/**
 * @Route("/{_locale}")
 */

方法

use Saxulum\RouteController\Annotation\Callback;
use Saxulum\RouteController\Annotation\Convert;
use Saxulum\RouteController\Annotation\Route;

/**
 * @Route("/hello/{name}",
 *      bind="hello_name",
 *      asserts={"name"="\w+"},
 *      values={"name"="world"},
 *      converters={
 *          @Convert("name", callback=@Callback("__self:convertName"))
 *      },
 *      method="GET",
 *      requireHttp=false,
 *      requireHttps=false,
 *      before={
 *          @Callback("__self:beforeFirst"),
 *          @Callback("__self::beforeSecond")
 *      },
 *      after={
 *          @Callback("__self:afterFirst"),
 *          @Callback("__self::afterSecond")
 *      }
 * )
 */
  • __self 被替换为控制器类。
  • __self:beforeFirst 在控制器实例上调用 beforeFirst 方法
  • __self::beforeSecond 在控制器上调用静态方法 beforeSecond

依赖注入注解

如果没有 DI 注解,则在没有任何依赖的情况下注册控制器,只要至少有一个方法路由注解即可。

容器注入

use Saxulum\RouteController\Annotation\DI;

/**
 * @DI(injectContainer=true)
 */

服务注入

use Saxulum\RouteController\Annotation\DI;

/**
 * @DI(serviceIds={"url_generator"})
 */