Zend:重写语言URL以纠正控制器和操作?

时间:2012-08-12 14:02:45

标签: zend-framework plugins rewrite front-controller

我几个小时以来一直在挣扎,但我找不到解决办法。我正在尝试将我的SEO URL重写为正确的控制器和操作URL。

示例:

DE /Rezepte/Übersicht
EN /Recipes/Overview

两种情况下的控制器和操作都是/recipes/index。我发布了我发现并尝试使用的代码,但每次都会出现“找不到页面错误”:

$this->_getRequest($request);

$newController = $this->_checkController();
$newAction = $this->_checkAction($newController);

$router = Zend_Controller_Front::getInstance()->getRouter();

$route = new Zend_Controller_Router_Route_Static('' . $this->_controller . '/' . $this->_action, //this is url(www.url.com/login) that you want to rewrite, you can set whatever you want
array(
    'module' => 'default',
    'controller' => $newController,
    'action' => $newAction
));

$router->addRoute($newController. '-' . $newAction, $route);

$this->_getRequest($request);

我在前端控制器插件中使用它。如果有人能给我一个解决方案的提示,那将是非常好的。

编辑: 这是完整的课程

    protected function _getRequest($request)
    {
        $this->_controller = $request->getControllerName();
        $this->_action = $request->getActionName();
    }

    protected function _buildPath()
    {
        $this->_path = APPLICATION_PATH . '/language/' . $this->_lang;
    }

    protected function _getLanguage()
    {
        $this->_lang = Zend_Registry::get('Zend_Locale')->getLanguage();
    }

    protected function _getConfigFiles()
    {
        $this->_cconfig = new Zend_Config_Ini($this->_path . '/urlparams.ini', 'controller');
        $this->_aconfig = new Zend_Config_Ini($this->_path . '/urlparams.ini', 'action');
    }

    protected function _checkController()
    {
        $array = $this->_cconfig->toArray();
        $key = array_search(strtolower($this->_controller), $array);

        if(empty($key))
            $key = 'index';
        return $key;
    }

    protected function _checkAction($controller)
    {
        $array = $this->_aconfig->toArray();
        $key = array_search(strtolower($this->_action), $array[$controller]);

        if(empty($key))
            $key = 'index';
        return $key;
    }

    public function __construct()
    {
        $this->_getLanguage();
        $this->_buildPath();
        $this->_getConfigFiles();
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $this->_getRequest($request);

        $newController = $this->_checkController();
        $newAction = $this->_checkAction($newController);

        $router = Zend_Controller_Front::getInstance()->getRouter();

        $route = new Zend_Controller_Router_Route_Static('' . $this->_controller . '/' . $this->_action, //this is url(www.url.com/login) that you want to rewrite, you can set whatever you want
            array(
                'module' => 'default',
                'controller' => $newController,
                'action' => $newAction
            ));

        $router->addRoute($newController. '-' . $newAction, $route);
    }
}

0 个答案:

没有答案