2012-02-16 23 views
2

我正在使用zend框架。在一个视图中,我使用了一个动作助手。防止直接访问动作控制器

echo $this->action('info', 'comment', null, array('articleID' => $article->arti_id));

因此,助手来电 'CommentController' 类和方法 'infoAction'。在我的方法里面,我得到了'articleID'参数,我开始用模型进行处理。最后我呈现一个共享视图。

public function infoAction() { 
    $articleID = $this->getRequest()->getParam('articleID'); 
    // Working with model 
      // ..... 
    $this->renderScript('/View/Script/DefaultComment.phtml'); 
} 

它的工作很好,但我不希望这个控制器/动作是由url domain.com/comment/info直接访问。

我该如何避免这种情况?

THX

回答

3

如果你的操作方法,总是有一个文章的ID参数调用,然后你可以重定向的情况下,没有ID的要求:

public function infoAction() { 
    if ($this->_hasParam('articleID')) { 
     $articleID = $this->getRequest()->getParam('articleID'); 
     // Working with model 
      // ..... 
    $this->renderScript('/View/Script/DefaultComment.phtml'); 
    } else { 
     $this->_helper->redirector('index', 'index', 'default'); 
    } 
} 
+0

是的,但是仍然可以访问这个URL为http: //domain.com/comment/info?articleID=1 – 2012-02-16 14:43:10

+1

确实如此,但由于您的网址在您的代码源中可见,因此不应该成为直接访问此页面的安全漏洞。但是,经纪人页面可能会发生...您必须知道action()非常昂贵,View Helper会更适合于实现这种操作。 – Liyali 2012-02-16 15:08:16

+0

最后,我创建了一个视图帮助器,并呈现我的视图。正如你所说,控制器必须保持公开/可访问。我的帮手更受保护。谢谢 – 2012-02-16 17:26:14