Yaf框架URL重写概述与配置方式
为实现更友好的URL结构,通常需要对站点URL进行重写处理。这一过程既可以在Web服务器(例如Nginx)的配置层面完成,也可以在应用程序代码层面实现。
本文主要介绍如何在Yaf框架环境下进行URL重写的相关操作,并对常见方法进行整理,以便后续查阅和使用。
Yaf框架支持多种URL重写策略,开发者可以根据实际需求选择单一方式或组合使用多种方式来配置路由。
第一种方式是通过修改配置文件来实现路由规则的定义。
;a rewrite route match request /product/*/*
routes.route_name.type="rewrite"
routes.route_name.match="/product/:name/:value"
routes.route_name.route.controller=product
routes.route_name.route.action=info
;a regex route match request /list/*/*
routes.route_name1.type="regex"
routes.route_name1.match="#^list/([^/]*)/([^/]*)#"
routes.route_name1.route.controller=Index
routes.route_name1.route.action=action
routes.route_name1.map.1=name
routes.route_name1.map.2=value
;a simple route match /**?c=controller&a=action&m=module
routes.route_name2.type="simple"
routes.route_name2.controller=c
routes.route_name2.module=m
routes.route_name2.action=a
;a simple router match /**?r=PATH_INFO
routes.route_name3.type="supervar"
routes.route_name3.varname=r
;a map route match any request to controller
routes.route_name4.type="map"
routes.route_name4.controllerPrefer=TRUE
routes.route_namer.delimiter="#!"
随后,需要在Bootstrap.php文件中添加一个初始化函数。该函数的名称可根据个人习惯自定义,但必须以下划线开头,这样才会被系统自动调用执行。
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract{
public function _initConfig() {
$config = Yaf_Application::app()->getConfig();
Yaf_Registry::set("config", $config);
}
public function _initRoute(Yaf_Dispatcher $dispatcher) {
$router = $dispatcher->getRouter();
/**
* we can add some pre-defined routes in application.ini
*/
$router->addConfig(Yaf_Registry::get("config")->routes);
/**
* add a Rewrite route, then for a request uri:
* http://***/product/list/22/foo
* will be matched by this route, and result:
*
* [module] =>
* [controller] => product
* [action] => info
* [method] => GET
* [params:protected] => Array
* (
* [id] => 22
* [name] => foo
* )
*
*/
$route = new Yaf_Route_Rewrite(
"/product/list/:id/:name",
array(
"controller" => "product",
"action" => "info",
)
);
$router->addRoute('dummy', $route);
}
?>
第二种方式是直接在程序代码中,以数组的形式配置路由规则。
上述数组配置方式的代码通常放置在Bootstrap.php文件中进行加载和初始化。
public function _initRoute(Ap_Dispatcher $dispatcher) {
//在这里注册自己的路由协议,默认使用static路由
$router = Ap_Dispatcher::getInstance()->getRouter();
$routeConfig = array(
$router = Ap_Dispatcher::getInstance()->getRouter();
$routeConfig = array(
"item" => array(
"type" => "regex",
"match" => "#^/(software|game)/(.*).html$#",
"route" => array('action' => 'item'),
"map" => array( 1 => 'data_type', 2 => 'docid' ),
),
//正则匹配
"category" => array(
"type" => "regex",
"match" => "#^/(software|game|video)/(.*)/(list_(.*).html)?$#",
"route" => array('action' => 'list' ),
"map" => array( 1 => 'data_type', 2 => 'cid',4 => 'page_num' ),
),
//使用动态结果 :a 表示action
"name" => array(
"type" => "rewrite", //Yaf_Route_Rewrite route
"match" => "/user-list/:a/:id", //match only /user-list/开头的
"route" => array(
'controller' => "user", //route to user controller,
'action' => ":a", //使用动态的action
),
),
);
$router->addConfig(new Ap_Config_Simple($routeConfig));
}
第三种方式涉及直接通过代码实例化路由对象并进行注册。
/**
* Add a rewrite route to Yaf_Router route stack
*/
Yaf_Dispatcher::getInstance()->getRouter()->addRoute("name",
new Yaf_Route_rewrite(
"/product/:name/:id/*", //match request uri leading "/product"
array(
'controller' => "product", //route to product controller,
),
)
);
以上内容涵盖了Yaf框架中URL重写的几种主要实现途径,希望能对读者的学习与实践提供帮助,同时也欢迎大家继续支持本站获取更多技术资源。