首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Yaf_Router (class)

介绍

(Yaf >=1.0.0)

Yaf_Router是标准的框架路由器。路由是指将URI端点(位于基础URI之后的URI的一部分:参见Yaf_Request_Abstract::setBaseUri())并将其分解为参数以确定该控制器的哪个模块,控制器和操作应接收请求。这个模块,控制器,动作和其他参数的值被打包到一个Yaf_Request_Abstract对象中,然后由Yaf_Dispatcher进行处理。路由只发生一次:最初收到请求时,第一个控制器被分派之前。

Yaf_Router旨在允许使用纯PHP结构的类似mod_rewrite的功能。它非常松散地基于Ruby on Rails路由,并且不需要任何关于web服务器URL重写的先前知识。它被设计为与单个Apache mod_rewrite规则(其中之一)一起工作:

示例#1重写Apache的规则

代码语言:javascript
复制
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php

or (preferred):

Example#2重写Apache的规则

代码语言:javascript
复制
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

如果使用Lighttpd,则以下重写规则有效:

Example#3为Lighttpd重写规则

代码语言:javascript
复制
url.rewrite-once = (
  ".*\?(.*)$" => "/index.php?$1",
  ".*\.(js|ico|gif|jpg|png|css|html)$" => "$0",
  "" => "/index.php"
)

如果使用Nginx,请使用以下重写规则:

Example#4重写Nginx的规则

代码语言:javascript
复制
server {
  listen ****;
  server_name  yourdomain.com;
  root   document_root;
  index  index.php index.html;

  if (!-e $request_filename) {
    rewrite ^/(.*)  /index.php/$1 last;
  }
}

默认路由

Yaf_Router预先配置了一个默认路由Yaf_Route_Static,它将匹配控制器/操作形式的URI。此外,可以将模块名称指定为第一个路径元素,从而允许表单module/controller/action的URI。最后,它还会匹配默认附加到URI的任何附加参数 - controller/action/var1/value1/var2/value2。

注意:模块名称必须在config中定义,考虑application.module =“Index,Foo,Bar”,在这种情况下,只有index,foo和bar可以被视为模块名称。如果没有配置,则只有一个名为“Index”的模块。

这些路线如何匹配的一些例子:

示例#5 Yaf_Route_Static(默认路由)示例

代码语言:javascript
复制
// Assuming the following configure:
$conf = array(
   "application" => array(
      "modules" => "Index,Blog",
   ),
);

Controller only:
http://example/news
    controller == news
Action only(when defined yaf.action_prefer=1 in php.ini)
    action  == news
 
Invalid module maps to controller name:
http://example/foo
    controller == foo
 
Module + controller:
http://example/blog/archive
    module     == blog
    controller == archive
 
Module + controller + action:
http://example/blog/archive/list
    module     == blog
    controller == archive
    action     == list
 
Module + controller + action + params:
http://example/blog/archive/list/sort/alpha/date/desc
    module     == blog
    controller == archive
    action     == list
    sort       == alpha
    date       == desc

类别简介

Yaf_Router {

/* Properties */

protected $_routes ;

protected $_current ;

/* Methods */

代码语言:javascript
复制
public bool addConfig ( Yaf_Config_Abstract $config )
代码语言:javascript
复制
public bool addRoute ( string $name , Yaf_Route_Abstract $route )
代码语言:javascript
复制
public __construct ( void )
代码语言:javascript
复制
public string getCurrentRoute ( void )
代码语言:javascript
复制
public Yaf_Route_Interface getRoute ( string $name )
代码语言:javascript
复制
public mixed getRoutes ( void )
代码语言:javascript
复制
public bool route ( Yaf_Request_Abstract $request )

}

属性

_routes

注册路线堆栈

_current

在路由阶段之后,这表明了哪个路由用于路由当前请求的名称。你可以通过Yaf_Router::getCurrentRoute()得到这个名字。

目录

  • Yaf_Router::addConfig - 将配置定义的路由添加到路由器中
  • Yaf_Router::addRoute - 将新路由添加到路由器中
  • Yaf_Router::__construct - Yaf_Router构造函数
  • Yaf_Router::getCurrentRoute - 获取有效的路由名称
  • Yaf_Router::getRoute - 按名称检索路由
  • Yaf_Router::getRoutes - 检索已注册的路由
  • Yaf_Router::route - 路由目的

← Yaf_Route_Rewrite::route

Yaf_Router::addConfig →

扫码关注腾讯云开发者

领取腾讯云代金券