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 7164743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:07:32+00:00 2026-05-28T14:07:32+00:00

I have a route with 2 parameters: BBBundle_blog_show: pattern: /{id}/{slug} defaults: { _controller: BloggerBlogBundle:Blog:show

  • 0

I have a route with 2 parameters:

BBBundle_blog_show:
    pattern:  /{id}/{slug}
    defaults: { _controller: BloggerBlogBundle:Blog:show }
    requirements:
        _method: GET
        id: \d+

Both params are properties of an object blog.

I would like to set up a custom mapper (route generator), so that I can write this:

 {{ path('BBBundle_blog_show', {'blog': blog}) }}

instead of this:

 {{ path('BBBundle_blog_show', {'id':blog.id, 'slug':blog.slug) }}
  • 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-05-28T14:07:32+00:00Added an answer on May 28, 2026 at 2:07 pm

    This is what I came up with eventually:

    I implemented by own generator base class that looks for ‘object’ parameter and tries to get required parameters from that object.

    //src/Blogger/BlogBundle/Resources/config/services.yml
    parameters:    
        router.options.generator_base_class: Blogger\BlogBundle\Routing\Generator\UrlGenerator
    
    //src/Blogger/BlogBundle/Routing/Generator/UrlGenerator.php
    
    namespace Blogger\BlogBundle\Routing\Generator;
    
    use Symfony\Component\Routing\Generator\UrlGenerator as BaseUrlGenerator;
    
    use Doctrine\Common\Util\Inflector;
    
    /**
     * UrlGenerator generates URL based on a set of routes.
     *
     * @api
     */
    class UrlGenerator extends BaseUrlGenerator
    {
        protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute)
        {
            if (isset($parameters['object']) && is_object($parameters['object'])) {
                $object = $parameters['object'];
                $parameters = array_replace($this->context->getParameters(), $parameters);
                $tparams = array_replace($defaults, $parameters);
                $requiredParameters = array_diff_key(array_flip($variables), $tparams);
    
                $parameters = $this->getParametersFromObject(array_flip($requiredParameters), $object);
            }
    
            return parent::doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute);
        }
    
        protected function getParametersFromObject($keys, $object)
        {
            $parameters = array();
            foreach ($keys as $key) {
                $method = 'get' . Inflector::classify($key);
                if (method_exists($object, $method)) {
                    $parameters[$key] = $object->$method();
                }
            }
    
            return $parameters;
        }
    
    }
    

    Now I can write: {{ path(‘BBBundle_blog_show’, {‘object’: blog}) }} and it will get required parameters (id, slug) from object.

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

Sidebar

Related Questions

I have the following route: get /:user_name/things/:thing_name => things#show, :as => show_user_thing Thing belongs
I have a route like following, ideally I would like it to match: domain.com/layout/1-slug-is-the-name-of-the-page
Let's say I have defined a route: routes.Add(new Route(Users/{id}, new MvcRouteHandler()) { Defaults =
Does outputcaching VaryByParams in webforms understand route parameters? Such that if I have a
can I have a route like... routes.MapRoute( Boundaries-Show, Boundaries, new { controller = Boundaries,
I want to define a route that have 2 optional parameters in the middle
I have this route routes.MapRoute( ViewGames, // Route name psp/{controller}/{action}, // URL with parameters
I have the following route routes.MapRoute(CreateBook, {controller}/{action}/{slug}/{name}, new { controller = CreateBook, action =
If I have a route with optional parameters, as follows: match 'comments/new(/:post_id/(/:parent_id))' => 'comments#new'
I have this route declaration: routes.MapRoute( // Route name WhiteLabelPartners, // URL with parameters

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.