Mostafa Solati
Mostafa Solati

Reputation: 1235

ZF2 navigation and sitemap

I have a class named MenuNavigatin and use it for top menu

class MenuNavigation extends DefaultNavigationFactory

Is it a good practice to query and add 500 products to this class to use this navigation for sitemap ?
Or should I have another class for sitemap and breadcrumbs ?
Or maybe add sitemap specific urls when generating sitemap

Upvotes: 0

Views: 387

Answers (1)

Jurian Sluiman
Jurian Sluiman

Reputation: 13558

Zend\Navigation tends to be slow for many pages. I would definitely not add all 500 pages to your navigation on every request. I'd add those pages only on a need-to-know base, which might be:

  1. For normal browsing, you probably don't need any of those products in your main menu, so you only might have pages like "home" and "products"
  2. When you select a product, you want to have it probably as sub-item of "products" so that "products" gets active in the menu when you visit a product. So create a page from your selected product and add that to the "products" parent when you dispatch this controller/action.
  3. For a sitemap you might want to include all the products to help search engines find your products. Only on this request you should parse the products and add them as pages.

For the third case, you could do various things to speed up this rendering:

  1. Cache the result of the sitemap, so next visist will have a fast response
  2. Warm-up the cache when something in your sitemap changes (which you might do asynchronously)

Furthermore, adding pages to the sitemap is a perfectly valid reason to enable an event manager. Your sitemap controller will trigger an event and your product module listens to the event. Next, it adds all those pages. This helps you to decouple your application and when you want to incorporate something alike (say, add 500 events as well), you could simply add a listener for your sitemap controller and have them added as pages as well.

Upvotes: 1

Related Questions