spiffy / spiffy-config
ZF2 模块,在控制器中直接提供路由注解。
Requires
- php: >=5.3.3
- doctrine/common: ~2.2
- symfony/finder: ~2.2
- zendframework/zend-cache: ~2.2
- zendframework/zend-code: ~2.2
- zendframework/zend-console: ~2.2
- zendframework/zend-log: ~2.2
- zendframework/zend-mvc: ~2.2
- zendframework/zend-serializer: ~2.2
- zendframework/zend-version: ~2.2
Requires (Dev)
- phpunit/phpunit: 3.*
- satooshi/php-coveralls: dev-master
- spiffy/spiffy-test: dev-master
- zendframework/zend-test: 2.*
This package is not auto-updated.
Last update: 2019-02-20 17:30:27 UTC
README
SpiffyConfig 是一个旨在加快配置的模块。
安装
SpiffyConfig 的安装使用 composer。有关 composer 文档,请参阅 getcomposer.org。
php composer.phar require spiffy/spiffy-config:dev-master
然后将 SpiffyConfig
添加到您的 config/application.config.php
文件中。
不使用 composer 的安装不受官方支持,需要您安装并自动加载 composer.json
中指定的依赖项。
最后,将 config/spiffyconfig.global.php.dist
复制到 autoload/spiffyconfig.global.php
目录。这将自动设置应用程序模块。
解析器
解析器将信息传递给构建器,以便构建器知道要处理什么。
- 文件:使用 Symfony 的文件查找器解析文件。
构建器
构建器从解析器获取信息,并根据该信息构建配置。
- RouteBuilder:读取路由注解并通过事件构建配置。
- ServiceBuilder:读取服务注解并通过事件构建配置。
- TemplateBuilder:从文件解析器获取文件并从中构建模板映射。
支持的注解
以下是当前支持的注解列表。随着更多注解的加入,此列表将更新。为了使用注解,您必须首先导入它们。通过在代码顶部放置以下内容来完成此操作,
use SpiffyConfig\Annotation as Config;
这将允许您在文档块中使用 @Config
来使用 SpiffyConfig 的注解。
服务
服务注解位于 SpiffyConfig\Annotation\Service
命名空间中,并处理在各个服务管理器上设置可调用实体和工厂。
属性
- key:定义配置时使用的键(默认:service_manager)。您可以使用竖线 “|” 将键嵌套在配置数组中,例如,my|nested 会将服务设置为:
php array( 'my' => array( 'nested' => array( // 服务配置将在这里 ) ) )
- shared:设置服务为共享或不共享(默认:null)
- type:通过使用
SpiffyConfig\Annotation\Service\Factory
或SpiffyConfig\Annotation\Service\Invokable
注解设置(默认:factories/invokables 分别对应) - name:用于服务的名称。如果没有指定名称,则使用 FQCN。
有几个注解扩展了服务注解并预先定义了 key
属性以节省您额外的步骤。每个这些都有 Factory 和 Invokable 注解可用。
服务注解
SpiffyConfig\Annotation\Controller
:键设置为controllers
SpiffyConfig\Annotation\Form
:键设置为form_elements
SpiffyConfig\Annotation\Hydrator
:键设置为hydrators
SpiffyConfig\Annotation\RouteManager
:键设置为route_manager
namespace Application\Service; use SpiffyConfig\Annotation\Service; /** * This annotation will set the Mailer service using the service_manager key * under the service name "Application\Service\Mailer". I could specify a name * if I don't want to use the FQCN. * * @Service\Invokable */ class Mailer implements ServiceLocatorAwareInterface { // ... implementation ... }
namespace Application\Service; use SpiffyConfig\Annotation\Service; /** * This annotation will set the Mailer service using the service_manager key * under the service name "mailer" and being built from the "Application\Service\MailerFactory" * factory. * * @Service\Factory("Application\Service\MailerFactory", name="mailer") */ class Mailer implements ServiceLocatorAwareInterface { // ... implementation ... }
路由
路由注解用于在控制器中直接设置路由。它们可以设置在类级别或方法级别。如果在类级别设置,必须指定与路由相关的动作。如果直接在方法上设置,则动作会为你设置。
目前,以下路由可用
- 通用:用于设置任何类型的路由。不直接管理。
- 文字:设置文字路由。
- 正则表达式:设置正则表达式路由。
- 段:设置段路由。
示例
<?php namespace Application\Controller; use SpiffyConfig\Annotation\Route; use Zend\Mvc\Controller\AbstractActionController; /** * @Route\Literal("/", name="home", action="index") */ class IndexController extends AbstractActionController { // This annotation is set on the controller level. public function indexAction() { // ... } /** * @Route\Segment("/foo/:id", name="foo", options={"constraints":{"id":"\d+"}}") */ public function fooAction() { // ... } /** * @Route\Literal("/bar", name="bar", parent="foo") */ public function barAction() { // ... } }
控制器
除了上述服务注解之外,控制器还有一个额外的 RouteParent
注解。此注解允许您为当前控制器中的所有动作设置父级。
namespace Application\Controller; use SpiffyConfig\Annotation\Controller; use SpiffyConfig\Annotation\Route; use Zend\Mvc\Controller\AbstractActionController; /** * @Controller\RouteParent("home", action="index") */ class IndexController extends AbstractActionController { /** * @Route\Literal("/") */ public function indexAction() { // ... } /** * Resolves to /foo and is named home/foo. * * @Route\Literal("foo", name="foo") */ public function fooAction() { // ... } }
选项
所有选项都在 SpiffyConfig\ModuleOptions
类中提供,并有详细描述。
命令行工具
提供了一个命令行工具来构建和清除缓存。在控制台运行您的 public/index.php
来查看相关信息。
自动路由名称
建议您为所有路由指定一个名称,例如,@Route\Literal("/", name="home")
。如果不这样做,则会基于控制器和动作名称的规范版本自动生成路由名称。
例如,如果您在 ControllerManager 中注册了一个控制器 My\Controller
并向 indexAction
添加路由,自动生成的路由名称将是 my_controller_index
。
Zend 开发者工具
如果您使用 ZendDeveloperTools,则提供了一个工具栏按钮,该按钮列出各种 SpiffyConfig 信息,并允许您快速访问使用密钥设置刷新页面。