2014-01-14 49 views

回答

0

有几种方法可以做到这一点...见下文。

我认为最好的方法是使用ACL MVC规则 - 为您的应用程序增加安全性,并可用于检查资源(模型/控制器/操作)是否存在。

选项1

建设类名,并得到它的方法,看看您的行为存在。无论是使用get_class_methodsReflectionClass::getMethods

/** 
* @param string $controller name of controller e.g. "index" 
* @param string $action name of action e.g. "index", "myAction" 
* @param string $module (optional) name of the current module 
* @return boolean 
*/ 
protected function _isControllerAction($controller, $action, $module = '') 
{ 
    $module = ($module == 'default') ? '' : $module; 
    $class = ucfirst($module) . ucfirst($controller) . 'Controller'; 
    $methods = get_class_methods($class); 
    return in_array("{$action}Action", $methods); 
} 

选项2

您可以检查模块/控制器的分派。这不会检查请求中的操作和混乱情况!如果你这样做,然后添加额外的代码来恢复请求状态。

protected function isDispatchableController() 
{ 
    $this->getRequest() 
      ->setModuleName('default') 
      ->setControllerName('index'); 

    $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher(); 

    /* @var $dispatcher Zend_Controller_Dispatcher_Standard */ 
    return $dispatcher->isDispatchable($this->getRequest()); 
} 

选项3

技术上你可以跳过这一切的检查和落实默认ErrorHandler,让它重定向到ErrorController ......然后添加特殊处理404

选项4

如果您使用ACL,您可以检查资源是否存在,并且用户是否有权访问它。 这里是好文章Zend ACL MVC Integration

0

如果你使用的是Zend Framework 2,这很简单。

假设我们要检查URI是否与注册路由器相匹配,并重定向用户,如果这与当前url不同。

$goto = 'http://www.mysite.tld/admin'; 

$request = $this->getRequest(); 
$request->setUri($goto); 

if ($routeToBeMatched = $this->getServiceLocator()->get('Router')->match($request)) { 
    $currentRouteMatchName = $this->getEvent()->getRouteMatch()->getMatchedRouteName(); 
    if ($routeToBeMatched->getMatchedRouteName() != $currentRouteMatchName) { 
     return $this->redirect()->toRoute($goto); 
    } 
} 
相关问题