更新:在 Zend Framework 中管理静态内容的最佳实践? [英] Updated: Best practices for managing static content in Zend Framework?

查看:23
本文介绍了更新:在 Zend Framework 中管理静态内容的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some questions concerning the Zend Framework. I am attempting to route all static pages through the default controller using the now default displayAction() method. The intention is to have the displayAction() process the request by looking at the page param, determine if the script page exists, if it does render the view otherwise throw a 404 page not found error. Additionally, a test is done to see if a method with the same name as the param exists, if so, call on that action.

此处列出的是来自 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);
    }
}

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

Now, here are my questions: Is there a better way of doing this? I'm relatively new to this framework, so are there best practices which apply? Is there a better way of calling on an action from within a controller? I have done A LOT of looking around through the documentation, however, quite a bit of it seems to contradict itself.

更新 1:

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

After having a think and a read, I've managed to simplify the solution and include a few things which were mentioned. NOTE: I use PagesController as my default static-content controller.

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

Listed here is the routing configuration from the application.ini. For calls to the home page i.e. "/", I pass "home" as the action param, for all other requests, the user defined / url link param is sent in 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.

Here is the controller actions. If user define parameter exists as an action, it will be called, else it falls to the php magic function __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 来清理输入是明智之举还是只是矫枉过正?

It works. So, here are my questions: Would you consider standardising on using this method to manage static content? If not, why not? How would you improve it? Also, considering this is a GET request, would it be a wise move to use Zend_Filter_input to cleanse input or is that just overkill?

推荐答案

我认为您在正确的轨道上,但这里有一些其他的想法.

I think your on the right track however here are some other ideas.

在您的 INI 中按部分分解您的路由:即博客路由器,静态页面路由器论坛路由器等..(我认为您已经这样做了)

Break up your routing per sections in your INI: ie a blog router, a static page router a forum router etc.. (I think you are already doing this)

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

Use the various router classes to handle routing per section rather than sending it to a controller.

静态: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

一些可能有帮助的链接:

Some links that may help:

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

这篇关于更新:在 Zend Framework 中管理静态内容的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆