Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top