今天我们主要讲解yaf的router路由的使用,路由的意义是自己能够定义自己url,即能简单的优化url方便用户记住,对seo也是很有帮助的,所以我们今天讲解的是yaf的路由的功能
Demo.php 插件实例代码
class Bootstrap extends Yaf_Bootstrap_Abstract{
public function _initConfig(Yaf_Dispatcher $dispatcher){
//var_dump($dispatcher->getRequest());
$config = Yaf_Application::app()->getConfig();
Yaf_Registry::set("config", $config);
}
public function _initRouter(){
//通过配置app.ini配置路由
$router = Yaf_Dispatcher::getInstance()->getRouter();
$router->addConfig(Yaf_Registry::get("config")->routes);
//自己设定路由
//$route = new Yaf_Route_Supervar("r");
//$route = new Yaf_Route_Simple("m","c","a");
/*
$router->addRoute(
"paging_example",
new Yaf_Route_Regex(
"#^/index/page/(d+)#",
array('controller' => "index"),
array(1 => "page")
)
);
*/
}
public function _initSpaceName(){
$loader = Yaf_Loader::getInstance()->registerLocalNamespace(array("Test","Dum"));
Yaf_Registry::set("loader", $loader);
}
public function _initPlugin(Yaf_Dispatcher $dispatcher){
$demo = new DemoPlugin();
$dispatcher->registerPlugin($demo);
}
}
app.ini配置
[app]
application.directory=APP_PATH "/app/"
application.view.ext="html"
application.library=APP_PATH "app/library/"
application.bootstrap=APP_PATH "Bootstrap.php"
application.dispatcher.catchException=1
;自定义路由
;;顺序很重要
routes.regex.type="regex"
routes.regex.match="#/list/([^/]*)/([^/]*)#"
routes.regex.route.controller=index
routes.regex.route.action=show
r<
application.view.ext="html"
application.library=APP_PATH "app/library/"
application.bootstrap=APP_PATH "Bootstrap.php"
application.dispatcher.catchException=1
outes.regex.map.1=name
routes.regex.map.2=value
;添加一个名为simple的路由协议
routes.simple.type="simple"
routes.simple.controller=c
routes.simple.module=m
routes.simple.action=a
;;添加一个名为supervar的路由协议
routes.supervar.type="supervar"
routes.supervar.varname=r
;
;[product : common]
;;product节是Yaf默认关心的节, 添加一个名为rewrite的路由协议
;routes.rewrite.type="rewrite"
;routes.rewrite.match="/product/:name/:value"