Tutorials For Database-Driven Routing in Zend Framework?

℡╲_俬逩灬. 提交于 2019-12-12 11:28:05

问题


I am working on a project that needs to use a database driven MVC scheme where the route to the controllers and views are controlled through a single database table. However, I haven't been able to find any tutorials that demonstrate this with a current version of the framework (they all appear to have been written several versions ago) and I was wondering if anyone has done something like this with a more recent version of the framework or if anyone knows of blogs or tutorials that discuss how to accomplish this in a simple manner.

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

Thanks in advance.


回答1:


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

You get a Zend_Controller_Request_Abstract object as the parameter of the "route" method. 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 define your route.

I hope it helps!




回答2:


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);
        }

     }

 }



回答3:


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

You can also get the same effect by using a common controller, all requests are routed to it then it can forwards to the right controller after talking to the database, although I prefer to use a plugin.

From the Manual:

preDispatch() is called before an action is dispatched by the dispatcher. This callback allows for proxy or filter behavior. By altering the request and resetting its dispatched flag (via Zend_Controller_Request_Abstract::setDispatched(false)), the current action may be skipped and/or replaced.

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



来源:https://stackoverflow.com/questions/382176/tutorials-for-database-driven-routing-in-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!