更新:在Zend Framework中管理静态内容的最佳实践


Updated: Best practices for managing static content in Zend Framework?

我有一些关于Zend框架的问题。我正在尝试使用现在默认的displayAction()方法通过默认控制器路由所有静态页面。其目的是让displayAction()通过查看page参数来处理请求,确定脚本页面是否存在,如果它确实呈现了视图,则抛出404页面未找到错误。此外,还会进行测试,查看是否存在与param同名的方法,如果存在,则调用该操作。

此处列出的是application.ini的路由配置

resources.router.routes.static-pages.route = /:page
resources.router.routes.static-pages.defaults.module = default
resources.router.routes.static-pages.defaults.controller = index
resources.router.routes.static-pages.defaults.action = display

以下是控制器的操作:

public function someAction() {
    // do something
}
public function displayAction() {  
    // extract page param, e.g. 'some'      
    $page = $this->getRequest()->getParam('page');
    // create zend styled action method, e.g. 'someAction'
    $page_action = $page.'Action';
    // if a method within this controller exists, call on it
    if (method_exists($this, $page_action)) {
        $this->$page_action();
    }
    // if nothing was passed in page param, set to 'home'
    if (empty($page)) {
        $page = 'home';
    }
    // if script exists, render, otherwise, throw exception.
    if (file_exists($this->view->getScriptPath(null)."/".$this->getRequest()->getControllerName()."/$page.".$this->viewSuffix)) {
        $this->render($page);
    } else {
        throw new Zend_Controller_Action_Exception('Page not found', 404);
    }
}

现在,我的问题是:有更好的方法吗?我对这个框架还比较陌生,所以有适用的最佳实践吗?有没有更好的方法从控制器内部调用操作?我在文档中做了很多浏览,然而,其中有相当一部分似乎自相矛盾。

更新1:

经过思考和阅读,我设法简化了解决方案,并包含了一些提到的内容。注意:我使用PagesController作为默认的静态内容控制器。

这里列出的是application.ini中的路由配置。对于主页的调用,即"/",我将"home"作为action参数,对于所有其他请求,用户定义的/url链接参数将在action中发送。

resources.router.routes.home.route = "/"
resources.router.routes.home.defaults.module = "default"
resources.router.routes.home.defaults.controller = "pages"
resources.router.routes.home.defaults.action = "home"
resources.router.routes.pages.route = "/:action"
resources.router.routes.pages.defaults.module = "default"
resources.router.routes.pages.defaults.controller = "pages"

以下是控制器的操作。如果用户定义参数作为一个操作存在,它将被调用,否则它将落入php魔术函数__call。

public function someAction()
{
    // Do something
}
public function __call($method, $args)
{
    // extract action param, e.g. "home"
    $page = $title = $this->getRequest()->getParam('action'); 
    // test if script exists
    if (file_exists($this->view->getScriptPath(null) . "/" 
        . $this->getRequest()->getControllerName() . "/$page . " . $this->viewSuffix)) 
   {
        // pass title to layout
        $this->view->assign(compact('title'));
        // render script
        $this->render($page);
    } else {
        throw new Zend_Controller_Action_Exception('Page not found', 404);
    }
}

它有效。所以,我的问题是:你会考虑标准化使用这种方法来管理静态内容吗?如果没有,为什么不呢?你将如何改进它?此外,考虑到这是一个GET请求,使用Zend_Filter_input来清除输入是明智之举,还是过于夸张了?

你的方法在我看来是合理的。然而,也许你应该利用__call方法,这将使你更容易地路由你的行动。。。

设置您的路线如下:

resources.router.routes.static-pages.route = /:action
resources.router.routes.static-pages.defaults.module = default
resources.router.routes.static-pages.defaults.controller = index

你的控制器是这样的:

public function someAction() {
    //going to URL /some will now go into this method
}
public function __call($name, $args) {
    //all URLs which don't have a matching action method will go to this one
}

我认为你的做法是正确的,但这里还有一些其他想法。

在INI中按节划分路由:即博客路由器、静态页面路由器、论坛路由器等。(我想你已经在做了)

使用各种路由器类来处理每个部分的路由,而不是将其发送到控制器。

静态:http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.static

全部:http://framework.zend.com/manual/en/zend.controller.router.html

一些可能有帮助的链接:

  • codeutopia.net/blog/2007/11/16/routing-and-complex-urls-in-zend-framework/
  • www.vayanis.com/2009/03/20/intro-to-zend-framework-routing/