0

无论如何创建一个虚 url “catch all” 路由,同时保持默认的 /module/controller/action/value 路由结构?

谢谢!

4

2 回答 2

3

如果您设置自定义路线会更好,例如在您的引导程序中:

protected function _initRoutes() {
     $this->bootstrap('frontController');
     $front = $this->getResource('frontController');
     $router = $front->getRouter();

     $router->addRoute(
         'neat_url',
         new Zend_Controller_Router_Route(
              'profile/:username',
              array(
                   'controller' => 'profiles',
                   'action' => 'view_profile'
              )
         )
     );
}

这样,您仍然可以拥有默认路由并拥有自定义路由,该路由将重定向 /profile/jhon.doe 下的所有内容,然后在您的控制器下,您可以使用 $this->_getParam('username'); 获取参数

于 2009-08-19T16:30:59.333 回答
2

您可以在前端控制器插件上使用 PreDispatch() 挂钩。像这样:

在您的引导程序中

<?php
...
$frontController = Zend_Controller_Front::getInstance();
// Set our Front Controller Plugin
$frontController->registerPlugin(new Mystuff_Frontplugin());

?>

然后在 Mystuff/Frontplugin.php 里面

<?php

class Mystuff_Frontplugin extends Zend_Controller_Plugin_Abstract
{
    ....

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        ....

        $controllerFile = $this->_GetRequestedControllerFile();

        if (!is_file($controllerFile)) {
            // Controller does not exist
            // re-route to another location
            $request->setModuleName('customHandler');
            $request->setControllerName('index'); 
            $request->setActionName('index');

        }
    }

    ....
}

preDispatch() 也是处理应用程序范围身份验证的方便位置。

于 2009-08-19T03:53:49.643 回答