Php 更新:Zend Framework中管理静态内容的最佳实践?
我有一些关于Zend框架的问题。我正在尝试使用现在默认的Php 更新:Zend Framework中管理静态内容的最佳实践?,php,zend-framework,Php,Zend Framework,我有一些关于Zend框架的问题。我正在尝试使用现在默认的displayAction()方法通过默认控制器路由所有静态页面。其目的是让displayAction()通过查看页面参数来处理请求,确定脚本页面是否存在,如果它确实呈现视图,否则抛出404页面未找到错误。此外,还进行了一个测试,以查看是否存在与param同名的方法,如果存在,则调用该操作 这里列出的是来自应用程序ini的路由配置 resources.router.routes.static-pages.route = /:page res
displayAction()
方法通过默认控制器路由所有静态页面。其目的是让displayAction()
通过查看页面
参数来处理请求,确定脚本页面是否存在,如果它确实呈现视图,否则抛出404页面未找到错误。此外,还进行了一个测试,以查看是否存在与param同名的方法,如果存在,则调用该操作
这里列出的是来自应用程序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”作为操作
参数传递,对于所有其他请求,用户定义的/url链接参数在操作
中发送
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"
以下是控制器的操作。如果user define参数作为一个动作存在,它将被调用,否则它将被php神奇函数调用
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来清理输入是明智之举还是过激?我认为您的方法是合理的。但是,也许您应该利用
\u 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中的每个部分分解路由: ie博客路由器、静态页面路由器、论坛路由器等。。(我想你已经这样做了) 使用各种路由器类来处理每个部分的路由,而不是将其发送到控制器 静态: 全部: 一些可能有帮助的链接:
- codeutopia.net/blog/2007/11/16/routing-and-complex-url-in-zend-framework/
- www.vayanis.com/2009/03/20/intro-to-zend-framework-routing/