8000 [TwigBridge][RFC] Automatically extract route params from objects in path() and url() · Issue #25276 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBridge][RFC] Automatically extract route params from objects in path() and url() #25276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vudaltsov opened this issue Dec 2, 2017 · 4 comments
Labels

Comments

@vudaltsov
Copy link
Contributor
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? yes
Symfony version 4.1

This is an idea on how to replace this

{{ path('app_news', {alias: news.alias}) }}
{{ path('app_blog_post', {id: blog.id}) }}

with

{{ path('app_news', news) }}
{{ path('app_blog_post', blog) }}
<?php

namespace Symfony\Bridge\Twig\Extension;

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class RoutingExtension extends AbstractExtension
{
    private $generator;
    private $router;
    private $accessor;

    public function __construct(UrlGeneratorInterface $generator, RouterInterface $router, PropertyAccessorInterface $accessor)
    {
        $this->generator = $generator;
        $this->router = $router;
        $this->accessor = $accessor;
    }

    public function getFunctions()
    {
        return array(
            // same for url
            new TwigFunction('path', array($this, 'getPath')),
        );
    }

    public function getPath($name, $parameters = array(), $relative = false)
    {
        if (is_object($parameters)) {
            $parameters = $this->getObjectParameters($name, $parameters);
        }

        return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
    }

    private function getObjectParameters($name, $object)
    {
        $parameters = [];

        $route = $this->router
            ->getRouteCollection()
            ->get($name);

        if (null !== $route) {
            foreach ($route->compile()->getVariables() as $variable) {
                if ($this->accessor->isReadable($object, $variable)) {
                    $parameters[$variable] = $this->accessor->getValue($object, $variable);
                }
            }
        }

        return $parameters;
    }
}
@vudaltsov vudaltsov changed the title [TwigBridge][RFC] Allow to pass objects to [TwigBridge][RFC] Automatically extract route params from objects in path() and url() Dec 2, 2017
@Koc
Copy link
Contributor
Koc commented Dec 3, 2017

duplicates #10395, #2288, #5999

@vudaltsov
Copy link
Contributor Author

@Koc , thanx, got it :)

@zmitic
Copy link
zmitic commented Jan 4, 2018

@vudaltsov In case you are still looking for a solution, check the bundle I released today:

https://github.com/zmitic/rewire-bundle

It works by adding one annotation in your route and I didn't notice any perfomance issues for a page with around 100 links (didn't test for more).

Feedback is more than welcome.

@vudaltsov
Copy link
Contributor Author

@zmitic , thanx. I will try it later and give feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants
0