帮助创建Zend路由器插件以满足特殊要求

时间:2011-09-16 20:56:47

标签: php zend-framework zend-route

当有人写下以下

时,zend路由器应该是什么
http://domain.com/admin/news/edit/id/6

最后它路由到以下

http://domain.com/news/admin/edit/id/6

我可以使用任何控制器插件来执行此操作吗?

请帮帮我......

1 个答案:

答案 0 :(得分:1)

我没有得到任何人的帮助。但最后我设法创建了一个控制器插件来解决这个问题。我发布这个,因为它可以帮助任何人...

class Layzend_Controller_Plugin_AdminRouter extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $module = $request->getModuleName(); 
        $controller = $request->getControllerName(); 
        $action = $request->getActionName();

        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
        $options = $bootstrap->getOption('custom');
        $adminDirectory = $options['adminDirectory'];
        $adminDirectory = $adminDirectory ? $adminDirectory : 'admin';

        if($module == $adminDirectory)
        {
            $newModule = ($controller == 'index') ? 'default' : $controller;
            $newController = 'admin';
            $newAction = $action;

            $moduleDir = APPLICATION_PATH . "/modules/$newModule";
            if(!is_dir($moduleDir)) 
            {
                Zend_Layout::getMvcInstance()->setLayoutPath(APPLICATION_PATH . "/layouts/scripts/admin/");
                throw new Zend_Controller_Action_Exception('Page not found.',404);
            }

            $request->setModuleName($newModule);
            $request->setControllerName($newController);
            $request->setActionName($newAction);

        } 
        else if($module == 'admin') {
            throw new Zend_Controller_Action_Exception('Page not found.',404);
        }
    }
}