secit-pl / route-injector-bundle
路由注入 Symfony 扩展包。
2.0.1
2023-08-30 11:33 UTC
Requires
- php: >=7.2
- jms/metadata: ^2.0
- symfony/dependency-injection: ^4.0|^5.0|^6.0
- symfony/framework-bundle: ^4.0|^5.0|^6.0
This package is not auto-updated.
Last update: 2024-09-11 16:21:02 UTC
README
为 Symfony 4.0+ 提供路由注入注解。
对于 Symfony 2.8 和 3.x,请使用 1.x 版本。
安装
在命令行运行
$ composer require secit-pl/route-injector-bundle
用法
注解
要将路由注入到变量中,只需在该变量上添加 @InjectRoute 注解。第一个参数是路由名称。第二个和后面的参数是可选的配置选项。
use SecIT\RouteInjectorBundle\Mapping\Annotation\InjectRoute; /** * @InjectRoute("route_name", configuration options...); */ private $url;
基本用法
以下是一个基本用法示例。
类
use SecIT\RouteInjectorBundle\Mapping\Annotation\InjectRoute; class Example { /** * @var string * * @InjectRoute("route_name", parametersMapping={"param": "getRouteParam"}); */ private $url; private $routeParam = ''; /** * @return string */ public function getUrl() { return $this->url; } /** * @return string */ public function getRouteParam() { return $this->routeParam; } /** * @param string $value */ public function setRouteParam($value) { $this->routeParam = $value; } }
以及如何手动运行注入器。
$processor = $this->container->get('secit.route_injector.processor'); $exampleEbject = new Example(); $exampleEbject->setRouteParam('test'); $processor->injectRoutes($exampleEbject);
Doctrine 实体用法
此扩展包提供了 Doctrine 集成。因此,如果在实体中使用 @InjectRoute 注解,则每次(根据条件)加载、更新和持久化操作都会触发 injectRoutes 处理器方法。
示例实体
namespace ExampleBundle\Entity; use Doctrine\ORM\Mapping as ORM; use SecIT\RouteInjectorBundle\Mapping\Annotation\InjectRoute; /** * @ORM\Table(name="example") * @ORM\Entity() */ class Example { /** * @var string * * @ORM\Column(name="param", type="string") */ private $param; /** * @var string * * @InjectRoute("route_name", parametersMapping={"param": "getParam"}); */ private $url; /** * @return string */ public function getParam() { return $this->string; } /** * @param string $param */ public function setParam($param) { $this->string = $param; } /** * @return string */ public function getUrl() { return $this->url; } }
当你创建一个新的 Example 实体实例时,$url 将为 null。但是,在持久化之后,路由将被自动注入并可用。
$exampleObject = new ExampleBundle\Entity\Example(); $this->getDoctrine()->getManager()->persist($exampleObject);
材料化路由
在上一个示例中,路由将在每次加载、更新或存储实体时注入。但在某些情况下,我们希望将其存储在数据库中。为此,只需添加 Doctrine @Column 注解,使其成为数据库字段。
namespace ExampleBundle\Entity; use Doctrine\ORM\Mapping as ORM; use SecIT\RouteInjectorBundle\Mapping\Annotation\InjectRoute; /** * @ORM\Table(name="example") * @ORM\Entity() */ class Example { ... /** * @var string * * @ORM\Column(name="url", type="string", length=255) * @InjectRoute("route_name", parametersMapping={"param": "getParam"}); */ private $url; ... }
配置选项
以下是所有可能的配置选项
@InjectRoute("route_name", configuration options...);
parametersMapping = array, 默认为空数组
路由参数映射到公共类方法的数组,从中获取参数值。
@InjectRoute("route_name", parametersMapping={"param": "publicGetterMethodName"});
注意! 如果任何参数为 null,则不会注入路由。
absolute = bool, 默认为 false
如果希望生成的 URL 包含协议和主机名前缀,则设置为 true。
@InjectRoute("route_name", absolute=true);
injectIfNotEmpty = bool, 默认为 false
默认情况下,只有在当前值不为空(null、false 或空字符串)时才会注入路由。如果要使其可更新,即使它不为空,请将此选项设置为 true。
@InjectRoute("route_name", injectIfNotEmpty=true);