0

I have the following two Doctrine (ORM) entities:

  • Product
  • Category

Category contains one or more segment (level):

  • category
  • category/sub-category

Product contains a name and possibly a color. A Product either has a color or not.

  • product
  • product/red

I'd like to have both Category pages and Product pages.

The Category URL should be made up as {locale}/{category_path}, e.g.

  • /en_US/category
  • /en_US/category/sub-category

The Product URL should be made up as {locale}/{category_path}/{product_path}, e.g.

  • /en_US/category/product
  • /en_US/category/product/red
  • /en_US/category/sub-category/product
  • /en_US/category/sub-category/product/red

The problem with using Symfony's routing is that there maybe matching confusion between the following routes since they have the same number of segments:

  • /en_US/category/sub-category
  • /en_US/category/product

Is this something I can use the CMF DynamicRouter for? If so, at a high level, what pieces do I need to build? Do I need to use a RouteProvider for each Entity?

tomhv
  • 23
  • 6

1 Answers1

0

to use the dynamic router out of the box, your entities need to be able to implement the RouteReferrersReadInterface. That is, they need to return route objects. And you will need to implement the RouteProviderInterface so that you can find the documents in the database.

With the Doctrine ORM, you probably want separate route entities (one per language) that contain the manifested path and link to the products / categories. Afaik the ORM can only link between known entities - one way to use the same route object would be to make category and product share a common base class that is a Mapped Super Class. The product color variants could be done as a variable part of the URL, so you end up with a pattern saying .../{color}

With the CMF route enhancer, you can configure different controllers for category and product pages.

A limited example of ORM routes is provided with the RoutingBundle. You might extend the base entity, or do your own starting with that. The documentation on symfony.com is often first mentioning how to work with the default storage of the CMF, the Doctrine PHPCR-ODM, but things should work with the ORM as well. Open issues on the cmf github project if something does not work.

dbu
  • 1,497
  • 9
  • 8
  • Thanks, @dbu. In our existing system, we have a base Page class which is subclassed for each of the above (ProductPage and CategoryPage). I was hoping to remove that complexity with the dynamic router. I guess I'm not sure what makes the dynamic router "dynamic" compared to the traditional Symfony router. – tomhv Aug 03 '15 at 17:07
  • not sure what you mean exactly. the idea of the dynamic router is to load routes from the database, so that you can write arbitrary seo friendly urls and store them. if you can always tell from a url whether its a product or a category, the "normal" configured routing is doing what you need already. what exactly did you hope to not need anymore? if ProductPage and CategoryPage have no individual fields you can drop them and just use the dynamic router. – dbu Aug 06 '15 at 16:28
  • Let me see if I understand. Let's say I have two Products and Two Categories. In symfony's built-in router, I'd have two routes, one for Product and one for Categories. If I use the dynamic router, do I have four routes? Are they cached? What if I add a new Product, does it automatically generate a new route? – tomhv Aug 07 '15 at 18:46
  • with the dynamic router, the editor could define the urls themselves, as in `/category-1/product-1/foobar` for the product 1 and `/seo-buzz/the-fancy-product` for product 2. editors can define any url they want. you don't need to have /category/category-1 and /product/product-1 urls. – dbu Aug 10 '15 at 08:29