开发者

How do I dynamically change a controller in Zend Framework?

开发者 https://www.devze.com 2022-12-12 12:23 出处:网络
I\'m halfway through a CMS where the URL is an SEO friendly name based on a page title. There is a need for one section to use a specific controller. So for example:

I'm halfway through a CMS where the URL is an SEO friendly name based on a page title. There is a need for one section to use a specific controller. So for example:

test.com/page1 (uses index controller) test.com/page2 (uses index controller) test.com/page3 (uses different controller) test.com/page4 (uses index controller)

I could add a route that says "page3" will use the "different" controller, but the users of the CMS need t开发者_StackOverflow社区o be able to change the name and seo of the URL, so where it is currently "page3" it could be changed later which would break my Routing rule.

What's the best way (either front controller plug in or other) to grab the request and pull the controller to be used from the DB (sql would be like "SELECT controller FROM menu WHERE seo = 'page3'"), then set that as the controller before Zend sets the controller to be used?

Any help or insight is greatly appreciated.


You will have to create a controller plugin and set the module/controller/action on the request object.

Then in the predispatch() you can do something like this:

public function preDispatch(Zend_Controller_Request_Abstract $request)
{
    $request->setModuleName($this->_getModule());
    $request->setControllerName($this->_getController());
    $request->setActionName($this->_getAction());
}

And then you can create methods __getModule(), _getController(), _getAction() that will examine the $_SERVER['REQUEST_URI'] and your DB and set the appropriate module/controller/action.

0

精彩评论

暂无评论...
验证码 换一张
取 消