Yii2获取URL和参数
1. 获取url中的host信息:
例如:http://www.nongxiange.com/product/2.html
Yii::$app->request->getHostInfo();
结果:http://www.nongxiange.com
2. 获取url中的路径信息(不包含host和参数):
例如:http://www.nongxiange.com/product/2.html?isnew=1
1
|
Yii:: $app ->request->getPathInfo() |
结果:product/2.html
3. 获取不包含host信息的url(含参数):
例如:http://www.nongxiange.com/product/2.html?isnew=1
Yii::$app->request->url
结果:product/2.html?isnew=1
4. 获取完整url(含host以及参数):
例如:http://www.nongxiange.com/product/2.html?isnew=1
1
|
Yii:: $app ->request->getHostInfo().Yii:: $app ->request->url; |
结果:http://www.nongxiange.com/product/2.html?isnew=1
5.只想获取url中的参数部分:
例如:http://www.nongxiange.com/product/2.html?isnew=1
1
|
Yii:: $app ->request->queryString |
结果:isnew=1
6.获取某个参数的值,比如id
例如:http://www.nongxiange.com/user?id=12345
1
|
Yii:: $app ->request->getQueryParam( 'id' ) |
结果:12345
假设被请求URL是 http://example.com/admin/index.php/product?id=100
Yii::$app->request->url;
//返回 /admin/index.php/product?id=100, 此URL不包括host info部分。
Yii::$app->request->absoluteUrl;
//返回 http://example.com/admin/index.php/product?id=100, 包含host infode的整个URL。
Yii::$app->request->hostInfo;
//返回 http://example.com, 只有host info部分。
Yii::$app->request->pathInfo;
//返回 /product, 这个是入口脚本之后,问号之前(查询字符串)的部分。
Yii::$app->request->queryString;
//返回 id=100,问号之后的部分。
Yii::$app->request->baseUrl;
//返回 /admin, host info之后, 入口脚本之前的部分。
Yii::$app->request->scriptUrl;
//返回 /admin/index.php, 没有path info和查询字符串部分。
Yii::$app->request->serverName;
//返回 example.com, URL中的host name。
Yii::$app->request->serverPort;
//返回 80, 这是web服务中使用的端口。