Zend Framework检查控制器是否存在


Zend Framework check if a controller exists or not

我正在寻找标题中提到的问题的解决方案。

我有ROUTER,我有一个订单,我检查控制器是否存在,但我找不到解决方案。

我做了类似的事情

class Plugin_Router extends Zend_Controller_Plugin_Abstract {
    public function routeStartup (Zend_Controller_Request_Abstract $request)
    {
        $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
        if (!$dispatcher->isDispatchable($request)) {
             // Controller exists
             // Exit ();
             $router = Zend_Controller_Front::getInstance()->getRouter();
             $router->addRoute('/:catid', new Zend_Controller_Router_Route('/:catid', array(
                 'module' => 'default' ,
                 'controller' => 'profile' ,
                 'action' => '' // Check your action and controller
             )));
         }
    }
}

如果它不起作用,还有其他解决方案吗?

这个插件应该做什么还不太清楚。您已经将逻辑放在routeStartup()中,这在路由发生之前发生。isDispatchable()在那里总是返回false,因为要调度的模块/控制器/操作尚未确定。

我会说将逻辑移动到routeShutdown()(在路由之后运行),但您的代码的目的似乎是在路由失败时添加一个新路由,这不会实现任何效果。你可能需要重新思考你的方法。

您可以在调度发生之前更改请求对象中的参数,这可能是您想要做的。

我找到了一半的答案:

public function routeStartup(Zend_Controller_Request_Abstract $request)
{
    $url = explode("/", substr($_SERVER['REQUEST_URI'], 1));
    $path = APPLICATION_PATH . '/controllers/' . ucfirst(strtolower($url[0])) .
        'Controller' . '.php';
    if (!file_exists($path)) {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        $router->addRoute(':catid', new Zend_Controller_Router_Route(':catid', array(
            'module' => 'default',
            'controller' => 'profile',
            'action' => 'index' // Check your action and controller
            )));
    }
}

}

如果有人在ZEND上有工作代码,希望听到。。。。

经过一些测试,我可以向您建议(这是受Dispatcher和Front Controller的启发):

public function routeStartup(Zend_Controller_Request_Abstract $request){
{
    //For feeding modules, controllers actions and parameters of $request
    $router = Zend_Controller_Front::getInstance()->getRouter();
    $router->setParams(Zend_Controller_Front::getInstance()->getParams());
    $router->route($request);
    // get Module and Controller names
    $dispatcher     = Zend_Controller_Front::getInstance()->getDispatcher();
    $controller     = $request->getControllerName();
    $default        = $dispatcher->formatControllerName($controller);
    $module         = $request->getModuleName();
    $controllerDirs = $dispatcher->getControllerDirectory();

    $found = false;
    $fileSpec = '';
    if ($dispatcher->isValidModule($module)) {        
        if (class_exists($default, false)) {
            $found = true;
        } else {
            $moduleDir = $controllerDirs[$module];
            $fileSpec  = $moduleDir . DIRECTORY_SEPARATOR . $dispatcher->classToFilename($default);
            if (Zend_Loader::isReadable($fileSpec)) {
                $found = true;                    
            }
        }
    }
    if ($found) {
    // The controller exists
    ...
    }
}