Tutorials for database oriented routing in Zend Framework?

I am working on a project that should use a database-driven MVC scheme, where the route to the controllers and views is controlled through a single database table. However, I couldn’t find any tutorials demonstrating this with the current version of the framework (they all seem to have been written a few versions ago), and I was wondering if anyone did similar similar to the later version or does anyone know blogs or guides that discuss how to do this in a simple way.

The main idea is that there will be a sitePage table that will contain the pageName, controller, module and view fields. When the request is processed, I need to query the database for the given page name and determine the appropriate controller, module and view, and then pass this to the necessary Zend class to continue the normal routing and processing of the request.

Thanks in advance.

+4
source share
3 answers

I realized that a more elegant approach really is to use a router, but for this you will need to create a custom one by extending the Zend_Controller_Router_Abstract class and implementing the route method.

As a parameter of the route method, you get the Zend_Controller_Request_Abstract object. There you can talk to the database, and then you can use:

Zend_Controller_Request_Abstract::setModuleName(), Zend_Controller_Request_Abstract::setControllerName(), Zend_Controller_Request_Abstract::setActionName() 

to determine the route.

Hope this helps!

+2
source

You can also use the routeStartup () method in your plugin. eg:

  class My_Plugin_PageRoute extends Zend_Controller_Plugin_Abstract { public function routeStartup () { $front = Zend_Controller_Front::getInstance(); $pages = new Model_Pages(); $page_data = $pages ->getPageInfo(); $router = $front->getRouter(); foreach($page_data as $page) { $r = new Zend_Controller_Router_Route( '' . $page -> page_name, array('controller' => 'pages', 'action' => 'index', 'page_id' => $page -> page_id) ); $router->addRoute('pages_' . $page -> page_id, $r); } } } 
+5
source

Perhaps the best aproach is not using routers, but using plugins or a common controller. Without a deeper analysis, I would suggest you create the Front Controller plugin, and then inside the preDispatch() method you can talk to the database and reset to request that it be sent to the right controller.

You can also get the same effect using a common controller, all requests are sent to it, after which it can go to the right controller after talking to the database, although I prefer to use the plugin.

From the manual:

preDispatch() is called before the dispatcher dispatches the action. This callback allows proxy or filter behavior. By changing the request and resetting its send flag (via Zend_Controller_Request_Abstract::setDispatched(false) ), the current action can be skipped and / or replaced.

http://framework.zend.com/manual/en/zend.controller.plugins.html

+2
source

All Articles