happyr / blaze-bundle
在一个地方编写你的路由映射
2.0.0
2020-01-10 17:44 UTC
Requires
- php: ^7.2
- symfony/config: ^4.4 || ^5.0
- symfony/dependency-injection: ^4.4 || ^5.0
- symfony/http-kernel: ^4.4 || ^5.0
- symfony/routing: ^4.4 || ^5.0
- symfony/yaml: ^4.4 || ^5.0
- twig/twig: ^2.1 || ^3.0
Requires (Dev)
- matthiasnoback/symfony-config-test: ^4.1
- nyholm/symfony-bundle-test: ^1.6.1
- symfony/framework-bundle: ^4.4 || ^5.0
- symfony/phpunit-bridge: ^4.4 || ^5.0
README
此捆绑包允许您配置动态路由。一段代码解释了其好处
// Generate the path /blog-post/{post_id}/comment/{comment_id}/edit <a href="{{ path('edit_comment', {'comment_id':comment.id, 'post_id':comment.post.id}) }}">Click here</a> <a href="{{ comment|blaze('edit') }}">Click here</a>
安装
步骤 1:使用 Composer
$ composer require happyr/blaze-bundle
步骤 2:注册捆绑包
注册捆绑包到您的内核
// in AppKernel::registerBundles() $bundles = array( // ... new Happyr\BlazeBundle\HappyrBlazeBundle(), // ... );
步骤 3:配置捆绑包
# app/config/config.yml happyr_blaze: objects: Acme\DemoBundle\Entity\Foo: edit: route: 'foo_edit' parameters: {id:'getId'} show: route: 'foo_show' parameters: {id:'getId'} Acme\DemoBundle\Entity\Bar: show: route: 'bar_show' parameters: {id:'getId'} Acme\DemoBundle\Entity\Baz: anything: route: 'baz_show' parameters: {id:'getId', foo_id:'getFoo.getId'} #if you need support for routes where the objects have no relation: Acme\DemoBundle\Entity\FooBar: manage: route: 'foobar_manage' parameters: [{id: 'getId'}, {baz_id: 'getId', baz_name: 'getName'}, {bazbar_id: 'getSlug'}] complementaryObjects: ["Acme\DemoBundle\Entity\Baz", "Acme\DemoBundle\Entity\BazBar"]
使用方法
Twig
{# foo is a Foo object #} <a href="{{ foo|blaze('show') }}">Show Foo</a> {# baz is a Baz object #} <a href="{{ baz|blaze('anything') }}">Show Baz</a> {# and the multiple objects .. #} <a href="{{ [foobar,baz,bazbar]|blaze('manage') }}">Show Baz</a>
PHP
use Happyr\BlazeBundle\Service\BlazeManagerInterface; class MyController { private $blaze; public function __construct(BlazeManagerInterface $blaze) { $this->blaze = $blaze; } public function SomeAction() { $blaze = $this->get(BlazeManagerInterface::class); $showUrl = $blaze->getPath($foo, 'show'); $manageUrl = $blaze->getPath($foobar, 'show', array($baz, $bazbar)); // ... } }