Zend Framework 的路由转发功能不仅可以让 url 变得简洁易记,而且非常对 SEO 非常有益,zf 的路由转发有着多样的配置方法,能实现各种转发需求。
随着站点复杂程度的增加,我们会有越来越多的转发规则,而将这些规则独立到一个文件中,是一个非常好的习惯。本文讲解的是将转发规则存放在单独的配置文件里面,然后配合 Zend_Config 实现转发的方法。
对于直接在程序中实现转发设置,手册上有简单的示例:
PHP:
- <?php
- $router = $controller->getRouter(); // 获取路由
- $route = new Zend_Controller_Router_Route(
- 'author/:username',
- array(
- 'controller' => 'profile',
- 'action' => 'userinfo'
- )
- );
- $router->addRoute('user', $route);
- ?>
上面的规则能将 http://domain.com/author/martel 指向到 http://domain.com/profile/action/author/martel ,这样设置,能大大降低 url 的复杂程度,让 url 简单明了。
我们如果有多条这种规则,就可以考虑将规则放到单独的文件中,手册上是用 ini 文件存储路由规则,我这里则使用 php 数组存储:
PHP:
- <?php
- return array(
- // 将 /view/123 映射为 /default/index/index/id/123
- 'view' => array(
- 'route' => 'view/:id',
- 'defaults' => array(
- 'module' => 'default',
- 'controller' => 'index',
- 'action' => 'index',
- ),
- ),
- // 将 http://domain.com/author/martel 映射为 http://domain.com/profile/action/author/martel
- 'profile' => array(
- 'route' => 'author/:username',
- 'defaults' => array(
- 'controller' => 'profile',
- 'action' => 'userinfo',
- ),
- ),
- );
- ?>
对于从数组中取出规则,然后设置 Zend_Controller_Router_Rewrite ,手册也有一段示例:
PHP:
- <?php
- $routerConfig = new Zend_Config(require_once 'routes.php');
- $routerRules = new Zend_Controller_Router_Rewrite();
- $router->addConfig($routerRules);
- ?>
不过,手册上没有如何将上面的路由规则应用到当前 controller 的示例,这个也是让新手很头疼的问题,研究过 zend framework 的源码后,我们可以很容易发现解决方法:
PHP:
- <?php
- $routerConfig = new Zend_Config('routes.php'); // 加载配置
- $routerRules = new Zend_Controller_Router_Rewrite();
- $routerRules->addConfig($routerConfig); // 设置规则
- $routers = $controller->getRouter(); // 获取路由
- $routers->addRoutes($routerRules->getRoutes()); // 通过 $routerRules->getRoutes() 获取规则,然后设置
- ?>
分享到:
相关推荐
•Zend_Config_Writer •Zend_Console_Getopt •Zend_Controller •Zend_Currency •Zend_Date •Zend_Db •Zend_Debug •Zend_Dojo •Zend_Dom •Zend_Exception •Zend_Feed •Zend_File •Zend_...
主要介绍了Zend Framework入门教程之Zend_Config组件用法,结合实例形式分析了Zend_Config组件针对各种类型配置文件操作的相关技巧,需要的朋友可以参考下
主要介绍了Zend Framework教程之Zend_Config_Ini用法,较为详细的分析了Zend_Config_Ini操作配置数据的相关技巧,需要的朋友可以参考下
5.3. Zend_Config_Ini 5.4. Zend_Config_Xml 6. Zend_Console_Getopt 6.1. Getopt 简介 6.2. 声明 Getopt 规则 6.2.1. 用短语法声明选项 6.2.2. 用长语法声明选项 6.3. 读取(Fetching)选项和参数 6.3.1. ...
zend framework中英文手册,包含中文手册,英文手册两个版本。
本文实例讲述了Zend Framework中Zend_Config_Xml用法。分享给大家供大家参考,具体如下: Zend_Config_Xml 让开发者能够存储配置数据到一个简单XML格式并通过嵌入对象属性语法来读取。 XML文件的根元素(root ...
Zend_framework_in_REST.pdf
Zend Framework+1.10 手册 英文版。Zend Framework+1.10 手册 英文版。
Zend Framework 中文 手册 2008年12月01日.chm MD5: DD29C39D12E9C0B38FD3475A12D05B73 SHA1: 1A6BBBF9311013F2FF2BF97752332160ED767526 CRC32: 88210ABD Zend Framework 入门教程(简体中文1.52版)v0.12.pdf MD5:...
Zend_Auth 和 Zend_Acl 通过通用 的证书(credential)存储提供用户认证和授权。还有其它实现的客 户库来简化访问最流行的可用的 web 服务。 不论你的程序需要什么,你都可能从 Zend Framework 中找 到全面测试的...
zend framework zendframework manual 手册,Zend Framework手册 中文版,有一部分还没有汉化
主要介绍了Zend Framework动作助手(Zend_Controller_Action_Helper)用法,详细分析了动作助手Zend_Controller_Action_Helper功能,定义,使用方法与相关实现代码,需要的朋友可以参考下
Whether you are learning Zend framework from scratch or looking to sharpen up your skills from previous versions, Zend Framework 2 Application Development will help you to harness the power of Zend ...
本文实例讲述了Zend Framework框架Zend_Db_Table_Rowset用法。分享给大家供大家参考,具体如下: 1. 简介 Zend_Db_Table_Rowset是 Zend_Db_Table_Row对象集合的迭代器.通常来说,你不可以自己实例化Zend_Db_Table_...
Zend Framework 2 官方教程汉化版
Zend_Framework_EN_20090313,Zend_Framework_EN_20090313