参考:
http://www.yiiframework.com/doc-2.0/guide-helper-url.html
use yii\helpers\Url;
//use yii\web\UrlManager;
http://www.yiiframework.com/doc-2.0/guide-helper-url.html
http://www.yiiframework.com/doc/guide/1.1/zh_cn/topics.url
<IfModule rewrite_module>
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
use yii\helpers\Url;
//use yii\web\UrlManager;
首页
$relativeHomeUrl = Url::home(); 相对
$absoluteHomeUrl = Url::home(true); 绝对
$httpsAbsoluteHomeUrl = Url::home('https'); 指定协议绝对
创建网址
$relativeHomeUrl = Url::home();
$absoluteHomeUrl = Url::home(true);
$httpsAbsoluteHomeUrl = Url::home('https');
echo Url::toRoute(['site/index', 'src' => 'ref1', '#' => 'name']);
echo Url::toRoute('site/index', true);
echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name']);
echo Url::current(); //需要 Yii版本 2.0.3
记忆网址
echo Url::remember();
echo Url::previous();
Url::remember(['product/view', 'id' => 42], 'product');
echo Url::previous('product');
网址重写
web.php
'components' => [
'urlManager' => [
'enablePrettyUrl' => true, //启用路由
'showScriptName' => false, //false为隐藏 index.php
//'suffix'=>'.html', //后缀
//规则
'rules' => [
''=>'site/index',
//'test/index/<id:\d+>/<page:\d+>'=>'test/index',
//'test/index/<id:\d+>/<cate>'=>'test/index',
'<_a:(test|test2)>/<id:\d+>' => '<_a>/index',
]
],
'test/index/<id:\d+>/<page:\d+>'=>'test/index',
Url::to(['test/index', 'id' => 12, 'page'=> 5])
result: /test/index/12/5
'test/index/<id:\d+>/<cate>'=>'test/index',
Url::to(['test/index', 'id' => 12, 'cate'=> 'bmw'])
resutl: /test/index/12/bmw
'<_a:(test|test2)>/<id:\d+>' => '<_a>/index',
Url::to(['test', 'id' => 12])
resutl: http://zhstory.cc/test/12