saeven / zf3-circlical-autowire
基于注解的路由,为 Laminas 和 ZF3 创建魔法控制器。
Requires
- php: ^7.4 | >=8
- doctrine/annotations: @stable
- laminas/laminas-cli: @stable
- laminas/laminas-code: 3.5.1
- laminas/laminas-config: @stable
- laminas/laminas-eventmanager: ^3.0.1 | ^3.3.1
- laminas/laminas-mvc: ^3.0.1 | ^3.2.0
- laminas/laminas-router: @stable
- laminas/laminas-servicemanager: ^3.0.1 | ^3.6.4
Requires (Dev)
- codacy/coverage: dev-master
- friends-of-phpspec/phpspec-code-coverage: @stable
- laminas/laminas-form: @stable
- phpspec/phpspec: 6.1.* | 7.0.1
This package is auto-updated.
Last update: 2024-09-20 14:06:53 UTC
README
一个支持快速开发的 laminas-mvc 模块,它做两件事
- 将路由编译成标准的 PHP 数组以用于生产(并自动为您合并)。不会与标准路由声明竞争(两者可以同时使用)。
- 自动将 DI 应用于您的控制器,您不需要编写大量的简单工厂。
旧版本支持 zend-mvc,请查看发行版。
在您的操作上方使用注解,可自动将路由插入 ZF3 路由器。无需再切换到路由文件或重新整理路由配置数组。
此模块还提供了一个基于反射的抽象工厂,可自动使用构造函数连接您的控制器。只需定义您的构造函数(就像您应该做的那样),然后让懒加载工厂完成其余工作!
## 安装
使用以下命令安装
composer require zf3-circlical-autowire
然后,在您的 application.config.php 文件中接近顶部添加它
'CirclicalAutoWire',
自动控制器 DI
无需做任何事情。它非常懒,是自动的。您可以为控制器编码、将依赖项添加到构造函数中,然后此模块将直接从您的服务容器中注入类到控制器中。
自动路由
在应使用此模块的任何控制器中,只需添加此 使用语句
use CirclicalAutoWire\Annotations\Route;
方法注解
在具有使用语句的控制器中的任何操作上,使用这些类型的注解
<?php
/**
* Your usual stuff here
* @returns bool
* @Route("/freedom")
*/
public function anyOldNameAction(){
// this beats editing a route file each time!
}
/**
* This route has a parameter
* @Route("/freedom/:param")
*/
public function anyOldNameAction(){
$this->params()->fromRoute('param');
}
/**
* This route has a parameter and a constraint
* @Route("/freedom/:param", constraints={"param":"[a-zA-Z]"})
*/
public function anyOldNameAction(){
// ...
}
/**
* Route with parameter, constraint, and defaults
* @Route("/freedom/:param", constraints={"param":"[a-zA-Z]"}, defaults={"param":"index"})
*/
public function anyOldNameAction(){
// ...
}
/**
* Route with parameter, name, constraint, and defaults
* @Route("/freedom/:param", name="easy-as-pie", constraints={"param":"[a-zA-Z]"}, defaults={"param":"index"})
*/
public function anyOldNameAction(){
// ...
}
子路由
定义子路由非常简单。通过给父路由一个名称以及它是否可以终止来定义父路由。单独地,告诉子路由它们的 parent
是第一个路由(通过名称)。以下是一个完整的示例
<?php
/**
* Class ChildRouteController
* @package Spec\CirclicalAutoWire\Controller
*/
class ChildRouteController extends AbstractActionController
{
/**
* @Route("/icecream", name="icecream", terminate=true)
*/
public function indexAction(){}
/**
* This is a sample docblock
*
* @Route("/eat", parent="icecream", name="eat")
*/
public function eatAction(){}
/**
* @Route("/select/:flavor", constraints={"flavor":"\d"}, name="select", parent="icecream")
*/
public function selectFlavorAction(){}
}
这将生成
<?php
'router' => [
'routes' => [
'icecream' => [
'type' => Literal::class,
'options' => [
'route' => '/icecream',
'defaults' => [
'controller' => ChildRouteController::class,
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'eat' => [
'type' => Literal::class,
'options' => [
'route' => "/eat",
'defaults' => [
'controller' => ChildRouteController::class,
'action' => 'eat',
],
],
],
'select' => [
'type' => Segment::class,
'options' => [
'route' => "/select/:flavor",
'defaults' => [
'controller' => ChildRouteController::class,
'action' => 'selectFlavor',
],
'constraints' => [
'flavor' => '\d',
],
],
],
],
],
],
],
控制器注解
作为便利提供,这有助于您达到更高的懒级别。如果您知道所有控制器路由都将以 /index/system
开头,只需将控制器按此方式注解即可
<?php
/**
* Controller Index
* @Route("/index/system")
*/
class IndexController extends AbstractActionController
{
/**
* @Route("/update")
*/
public function updateAction(){
}
/**
* @Route("/get/:id")
*/
public function getAction(){
}
}
在上面的示例中,将编译以下路由
- /index/system/update
- /index/system/get/:id
两种模式
模式通过配置设置
开发模式
在此模式下,会扫描注解并读取。扫描控制器代码的开销非常小。每次刷新时,您都会看到编译的路由文件在您指定的位置重新创建(请参阅此模块的配置文件)。
生产模式
在此模式下,不扫描注解。相反,它会自动将您在开发模式下创建的配置文件与您的 Zend 框架的 routes/route 配置合并,创建一个硬编码路由的“传统”场景。零模块开销。
重要提示:通过控制台激活时,它将表现得像生产模式一样
希望您喜欢这个模块,您可以在 freenode 的 #zftalk 或 Twitter 上的 @Saeven 上联系我!所有 PR 都会考虑!