路由是一个把请求URL映射到控制器的action上的工具,确定用户的请求该执行那个功能(或者反过来,某个action应该用哪个URL来访问到)。 路由从URL中解释并提取出这次请求的参数: 模块(module,简写mod)控制器(controller,简写ctl), 动作(action,简写act)以及其他参数。
第1步、我们在浏览器中打开URL: http://localhost/windwork/index.php?system.default.index
第2步、路由器解析请求URL中的参数:模块(module)、控制器(controller)、动作(action)和请求中的其他参数。这三个参数结果分别为
$mod = 'system';
$ctl = 'default';
$act = 'index';
即请求url可表示为: http://localhost/windwork/index.php?{$mod}.{$ctl}.{$act}
第3步、框架执行\module\{$mod}\controller\{$ctl}Controller::{$act}Action()方法, 即执行了 \module\system\controller\DefaultController::indexAction()
可以在URL中传递请求参数到action方法的参数。 请求URI格式为:{$baseUrl}{$mod}.{$ctl}.{$act}/参数1/参数2/…
namespace module\user\controller;
/**
* 传action参数
*/
class AccountController extends \wf\mvc\Controller {
/**
* 传参案例
*/
public function doAction($p1 = '', $p2 = ''){
// index.php?user.account.do/参数1/参数2/…
// $p1 == '参数1'; $p2 == '参数2';
// index.php?user.account.do/aa/bb
// $p1 == 'aa'; $p2 == 'bb';
// 我们还可以传更多的参数
// index.php?user.account.do/aa/bb/cc/dd....
}
}
请求URI格式为:{$baseUrl}{$mod}.{$ctl}.{$act}/param1/param2/下标1:值1/下标2:值2/… URL中的$key:$value键值对每个键值对直接用/分隔,冒号左边为下标,右边为值。 键值对数组保存在\wf\Request->vars中,在控制器中通过$this->request->getVar($key)或$this->request->getRequest($key)读取。
namespace user\controller;
/**
* 传URL请求变量
*/
class AccountController extends \wf\mvc\Controller {
/**
* 传变量演示说明
*/
public function loginAction($param = ''){
// index.php?user.account.login/name:demo/password:123456
$param == '';
$a = $this->request->getVar('name'); // $a == 'demo'
$b = $this->request->getVar('password'); // $b == '123456'
// index.php?user.account.login/vip/name:demo2/password:abc
$param == 'vip';
$a = $this->request->getRequest('name'); // $a == 'demo2'
$b = $this->request->getRequest('password'); // $b == 'abc'
}
}
$router = new \wf\mvc\Router();
$url = 'index.php?user.account.login/dopost/name:demo/password:123456&q1=xxx&q2=yyy#fragment'; // 未启用rewrite
$url = 'user.account.login/dopost/name:demo/password:123456?q1=xxx&q2=yyy#fragment'; // 启用rewrite
// 分析URL
$router->parseUrl($url);
// 分析后结果将被保存到args属性,结果为
$router->args == [
['mod'] => 'user',
['ctl'] => 'account',
['act'] => 'login',
['act_params'] => [
[0] => 'dopost',
],
['vars'] => [
['name'] => 'demo',
['password'] => '123456',
],
['?'] => 'q1=xxx&q2=yyy',
['#'] => 'fragment',
];
用\wf\mvc\Router::create()生成url,Router会自动把URl转成简写url,如果启用url重写,它也会自动生成重写的url。
/**
* 生成URL
* @param string $uri
* @param array $vars = array() 是否是完整URL
* @param bool $fullUrl = false 是否是完整URL
* @return string
*/
public static function create($uri, array $vars = array(), $fullUrl = false)
也可以用url()函数来生成URL。
/**
* 生成URL
* @param string $uri
* @param array $vars = array() 是否是完整URL
* @param bool $fullUrl = false 是否是完整URL
* @return string
*/
function url($uri, array $vars = array(), $fullUrl = 0) {
return \wf\mvc\Router::create($uri, $vars, $fullUrl);
}
可以设置简化URL让URL更简洁及更方便搜索引擎收录。 我们通过在系统管理后台进行设置简化URL规则, “登录管理后台 》设置 》链接设置 》 url简写规则” 设置URL简讯规则,每行一个规则,简写词开头,匹配规则在后面,以空格隔开。缩写词不能重复。 可使用的规则例如:
规则
demo1 mod.ctl.act/aboutUs/do:1/go:2
demo2 mod.ctl.act/aboutUs
demo3 mod.ctl.act
简化URL对应URL
index.php?demo1 => index.php?mod.ctl.act/aboutUs/do:1/go:2
index.php?demo2 => index.php?mod.ctl.act/aboutUs
index.php?demo3 => index.php?mod.ctl.act
index.php?demo1/a:b/c:d => index.php?mod.ctl.act/aboutUs/do:1/go:2/a:b/c:d
如果是在命令行下使用,请确定正确设置如下选项:
'host_info' => '', // 网站网址,如:http://www.yoursite.com:8080
'base_path' => '', // 网站根目录,如 /ctx/
'base_url' => '', // index.php? || empty string
通过URL Rewrite来移除 URL 中的 index.php,使URL看起来更简洁,也便于搜索引擎收录。 通过web服务器的URL重写,把类似http://yoursite.com/mod.ctl.act的链接重写到http://yoursite.com/index.php?mod.ctl.act,从而达到简化URL的效果。
#nginx URL重写设置
if (!-e $request_filename) {
rewrite ^/(.+)$ /index.php?$1 last;
}
# Apache URL重写设置
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
</IfModule>
IIS7+安装URL重写模块以后可支持URL重写,IIS7以下的版本需要另外下载安装URL重写控件到IIS。
#IIS 7+ URL 重写设置
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<defaultDocument>
<files>
<add value="index.php" />
</files>
</defaultDocument>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="256000000" />
</requestFiltering>
</security>
<urlCompression doStaticCompression="true" />
<rewrite>
<rules>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^(.*?)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>