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

Heg*_*mon 7 php zend-framework

我有一些关于Zend框架的问题.我正在尝试使用now default 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
Run Code Online (Sandbox Code Playgroud)

这是控制器动作:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是:有更好的方法吗?我对这个框架比较陌生,那么有适用的最佳实践吗?是否有更好的方法从控制器内调用操作?我已经做了大量的文档浏览,然而,相当多的它似乎自相矛盾.

更新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"
Run Code Online (Sandbox Code Playgroud)

这是控制器动作.如果用户定义参数作为动作存在,它将被调用,否则它将落入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);
    }
}
Run Code Online (Sandbox Code Playgroud)

有用.所以,我的问题是:您是否考虑使用此方法标准化来管理静态内容?如果没有,为什么不呢?你会如何改进它?另外,考虑到这是一个GET请求,使用Zend_Filter_input清理输入是一个明智的举动还是只是矫枉过正?

Jan*_*nen 1

你的做法对我来说似乎是合理的。但是,也许您应该利用__call方法,这将使您可以更轻松地路由您的操作......

像这样设置你的路线:

resources.router.routes.static-pages.route = /:action
resources.router.routes.static-pages.defaults.module = default
resources.router.routes.static-pages.defaults.controller = index
Run Code Online (Sandbox Code Playgroud)

你的控制器就像这样:

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
}
Run Code Online (Sandbox Code Playgroud)