Yii2 网址重写

参考:
http://www.yiiframework.com/doc-2.0/guide-helper-url.html

http://www.yiiframework.com/doc/guide/1.1/zh_cn/topics.url


.htaccess
<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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值