Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7966143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:20:42+00:00 2026-06-04T06:20:42+00:00

Sf2.0, standard blog for example. Code of routing.yml: DevBlogBundle_post_show: pattern: /posts/{id} defaults: { _controller:

  • 0

Sf2.0, standard blog for example.

Code of routing.yml:

DevBlogBundle_post_show:
 pattern:  /posts/{id}
 defaults: { _controller: "DevBlogBundle:Post:show" }
 requirements:
    _method:  GET
    id: \d+

Standard way i generate url for my post by using:

path('DevBlogBundle_post_show',{'id':post.id})

I use this constrution in all my temlates/layouts, which include posts list. An if i want to change my route for post_show (say… add Slug parameter /posts/{id}.{slug}), i will need to change all my temlates. Instead, i want to generate route by my Post model, something like:

public function getUrl(){
     return $this->generator->generate('DevBlogBundle_post_show',array (...params...));}

Question: How can i get This Generator to my Post model, what i have to “use …” and how to generate route?

In my templates i want to place:

<a href="{{ post.getUrl() }}" ...>...</a>

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:20:43+00:00Added an answer on June 4, 2026 at 6:20 am

    Excited by this question and the thoughts in my previous comment i’ve hacked together a quick proof of concept to achieve what you where asking for. I’ve implemented a twig function that can be passed an entity and a route name. The function does the following setps to generate the url:

    1. Get the route specified by name
    2. Compile the route
    3. Obtain the routes variables
    4. Loop over variables and try to call the according getters on the entity to build up the parameters array that is required by the UrlGeneratorInterface to generate a routes url
    5. Let symfony’s url generator generate the url

    However this implies that the routes parameters are named exactly as the properties in the entities, which IMHO is a good practice anyway. The code needs to be places into a twig extension that has the service container reference injected into the container property.

    public function generateEntityUrl($entity,$routeName)
    {
        $router         = $this->container->get('router');
        $generator      = $router->getGenerator();
        $collection     = $router->getRouteCollection();
        $route          = $collection->get($routeName);
        $compiledRoute  = $route->compile();
        $variables      = $compiledRoute->getVariables();
        $parameters     = array();
    
        foreach($variables as $var)
        {
            $getter = 'get'.ucfirst($var);
            $parameters[$var]=$entity->$getter();
        }
    
        return $generator->generate($routeName,$parameters);
    
    }
    

    In twig you can then call the function with {{post|entity_url('DevBlogBundle_post_show')}}.

    But I’m asking myself why there is no implementation yet in symfony… or why i have not found it yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following XML: <root> <StepFusionSet name=SF1> <StepFusionSet name=SF2> </StepFusionSet> </StepFusionSet> <StepFusionSet name=SF10> </StepFusionSet>
Couple of month ago I got a legacy project written on Sf2. I fixed
I'm currently integrating non symfony2 software (let's call it nsf ) in an sf2
I'm facing issues : in my security.yml I try to inject a parameter class
I'm getting this error when I try to clear the cache (for example): [Doctrine\ORM\Mapping
I'm building form using Sf2's form builder. public function buildForm(FormBuilder $builder, array $options) {
I registered my services.yml file like below : services: PMI.form.users_tasks: class: PMI\UserBundle\Form\UsersTasksType arguments: EntityManager:
I'm having trouble figuring out how to use soundfonts on android (that have a
I downloaded the latest symfony2 (2.0.10) and tried to run bin/vendors install but it
I'm trying to create my own library in a Symfony2 project but I'm having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.