1

I have a question about zend framework 2 router. I have a table "seourl" in mysql like that:

url module controller action param test123 catalog product view 5 abc123 catalog product view 6 other catalog category view 10 

I want to include these urls in router.

In the url field i can have url like that: others/product/ (i want to route any type of url from this table)

Thanks in advance.

Later Edit:

I want to route each url from this table.

Example:

example.com/test123 will load module catalog / controller product / action view / param 5

example.com/other will load module catalog / controller category / action view / param 10

4
  • If you want route any kind of url, add a column "module". What's your question ? Howto ? Commented Jun 7, 2013 at 7:37
  • @RemiThomas just edited table (i changed controller in module). Commented Jun 7, 2013 at 14:46
  • No I would say url|module|controller|action|param Commented Jun 7, 2013 at 14:48
  • @RemiThomas ok. is not a problem with sql (just updated table). i have problems with part of implemet in router. Commented Jun 7, 2013 at 15:02

1 Answer 1

4
+50

One way to do this is to attach an event (with a priority > 0, which is important!) to the application's 'route' event. This positive priority will cause the handler to be executed before route-matching occurs, which means you have the opportunity to add your own routes.

Something like the following. Keep in mind that this is not tested anywhere, so you may need to clean some things up.

<?php namespace MyApplication; use \Zend\Mvc\MvcEvent; use \Zend\Mvc\Router\Http\Literal; class Module { public function onBootstrap(MvcEvent $e){ // get the event manager. $em = $e->getApplication()->getEventManager(); $em->attach( // the event to attach to MvcEvent::EVENT_ROUTE, // any callable works here. array($this, 'makeSeoRoutes'), // The priority. Must be a positive integer to make // sure that the handler is triggered *before* the application // tries to match a route. 100 ); } public function makeSeoRoutes(MvcEvent $e){ // get the router $router = $e->getRouter(); // pull your routing data from your database, // implementation is left up to you. I highly // recommend you cache the route data, naturally. $routeData = $this->getRouteDataFromDatabase(); foreach($routeData as $rd){ // create each route. $route = Literal::factory(array( 'route' => $rd['route'], 'defaults' => array( 'module' => $rd['module'], 'controller' => $rd['controller'], 'action' => $rd['action'] ) )); // add it to the router $router->addRoute($route); } } } 

That should ensure that your custom routes are added to the router before the application attempts to find a routeMatch.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for response. i have a question. if i will have 10k urls ?
With 10k URLs, you might want to do things differently. Instead of adding one literal route per URL, consider creating your own Route type by implementing RouteInterface.
Can you send me an example ? I'm new in zend framework 2. Thanks!
If a route have child routes how can we achive that ?