ckrack/optimus-bundle

在 Symfony 项目中集成 Optimus

安装: 6

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

v1.0 2020-06-30 13:47 UTC

This package is auto-updated.

Last update: 2024-09-29 05:55:37 UTC


README

在 Symfony 项目中集成 jenssegers/optimus

使用 composer 安装

以下命令要求您已全局安装 Composer。打开命令行,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本

使用 Symfony Flex

    composer config extra.symfony.allow-contrib true
    composer require ckrack/optimus-bundle

使用 Symfony 4 框架

    composer require ckrack/optimus-bundle

如果此操作未自动完成,请在项目的 config/bundles.php 文件中添加以下行以启用此包

<?php

return [
    …,
    Ckrack\OptimusBundle\CkrackOptimusBundle::class => ['all' => true],
];

配置

配置 (config/packages/ckrack_optimus.yaml) 如下所示

ckrack_optimus:
    # Set options, as documented at https://github.com/jenssegers/optimus#usage
    prime: "%env(int:OPTIMUS_PRIME)%"
    inverse: "%env(int:OPTIMUS_INVERSE)%"
    random: "%env(int:OPTIMUS_RANDOM)%"

    # if set to true, param converter will continue with the next available param converters
    passthrough: true

要生成环境变量,我们可以使用 optimus 的 spark 命令。

vendor/bin/optimus spark -f env

用法

$optimus = $this->get('optimus');

Optimus 参数转换器

转换器名称: optimus.converter

Optimus 参数转换器尝试将路由中设置的 optimus 属性转换为整数参数。

/**
 * @Route("/users/{optimus}")
 */
public function getAction(int $optimus)
{
}

对于特定情况,只需在参数转换器选项中添加 "optimus" = "{parameter_name}" 即可。

/**
 * @Route("/users/{user}")
 * @ParamConverter("user", options={"optimus" = "user"})
 */
public function getAction(int $user)
{
}

使用 Passthrough

Passthrough 允许继续使用下一个可用的参数转换器。因此,如果您想获取实体而不是整数,只需激活 passthrough。

ckrack_optimus:
    passthrough: true

基于上面的示例

/**
 * @Route("/users/{user}")
 * @ParamConverter("user")
 */
public function getAction(User $user)
{
}

如你所见,passthrough 功能允许使用 DoctrineParamConverter 或任何其他你创建的 ParamConverter

Twig 扩展

Twig 扩展提供了在模板中生成 optimus 的功能。

用法

{{ path('users.show', {'optimus': user.id | optimus }) }}