ckrack / optimus-bundle
Integrate Optimus in a Symfony project
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=7.2
- jenssegers/optimus: ^1.1
- sensio/framework-extra-bundle: ~3.0
- symfony/http-kernel: ^4.0 || ^5.0
Requires (Dev)
- bossa/phpspec2-expect: ^3.0
- ergebnis/phpstan-rules: ^0.15.0
- friendsofphp/php-cs-fixer: ^2.16.3
- phpspec/phpspec: ^4.3 || ^5.1 || ^6.1
- phpstan/phpstan: ^0.12.31
- phpstan/phpstan-strict-rules: ^0.12.2
- rector/rector: ^0.7.37
- roave/security-advisories: dev-master
- thecodingmachine/phpstan-strict-rules: ^0.12.0
- twig/twig: ^2.7
Suggests
- twig/twig: Allows to use optimus in Twig template engine
This package is auto-updated.
Last update: 2024-11-29 06:22:01 UTC
README
Integrates jenssegers/optimus in a Symfony project.
Installation using composer
These commands requires you to have Composer installed globally. Open a command console, enter your project directory and execute the following commands to download the latest stable version of this bundle:
Using Symfony Flex
composer config extra.symfony.allow-contrib true
composer require ckrack/optimus-bundle
Using Symfony 4 Framework
composer require ckrack/optimus-bundle
If this has not been done automatically, enable the bundle by adding the
following line in the config/bundles.php
file of your project:
<?php return [ …, Ckrack\OptimusBundle\CkrackOptimusBundle::class => ['all' => true], ];
Configuration
The configuration (config/packages/ckrack_optimus.yaml
) looks as follows:
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
To generate the env variables, we can use optimus' spark
command.
vendor/bin/optimus spark -f env
Usage
$optimus = $this->get('optimus');
Optimus Param Converter
Converter Name: optimus.converter
The optimus param converter attempts to convert optimus
attribute set in the route into an integer parameter.
/** * @Route("/users/{optimus}") */ public function getAction(int $optimus) { }
For specific case, just add "optimus" = "{parameter_name}"
in ParamConverter
options:
/** * @Route("/users/{user}") * @ParamConverter("user", options={"optimus" = "user"}) */ public function getAction(int $user) { }
Using Passthrough
Passthrough
allows to continue with the next available param converters.
So if you would like to retrieve an Entity instead of an integer, just activate
passthrough :
ckrack_optimus: passthrough: true
Based on the example above:
/** * @Route("/users/{user}") * @ParamConverter("user") */ public function getAction(User $user) { }
As you can see, the passthrough feature allows to use DoctrineParamConverter
or any another ParamConverter
you would have created.
Twig Extension
The Twig extension provides optimus generation inside of templates.
Usage
{{ path('users.show', {'optimus': user.id | optimus }) }}