php 如何直接从查询字符串(URL)中获取 Yii 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21309458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Yii parameters directly from the querystring (URL)
提问by Zabs
I am using the latest version of Yii and trying to basically collate the URL parameters.
我正在使用最新版本的 Yii 并尝试基本上整理 URL 参数。
For instance if my URL was as follows:
例如,如果我的网址如下:
site.com/events/199/111
What is the function to grab the first parameter e.g '199' and similarly again to say grab the 2nd parameter e.g '111'.
获取第一个参数(例如“199”)的函数是什么,同样再次说获取第二个参数(例如“111”)的功能是什么。
I remember CodeIgniter has $this->url->segment(1), I basically need the same functionality to Yii :)
我记得 CodeIgniter 有$this->url->segment(1),我基本上需要与 Yii 相同的功能:)
回答by Rohan
On somthing like -
在诸如此类的事情上 -
$this->createUrl('blah/blahx',array('id'=>2));
You can get your parameter using -
您可以使用 -
$x = CHttpRequest::getParam('id'); //outputs x=2
OR $x = Yii::app()->getRequest()->getQuery('id'); //x=2 again
或者 $x = Yii::app()->getRequest()->getQuery('id'); //x=2 again
回答by Jason W
you can try getParam method
你可以试试 getParam 方法
$id = Yii::app()->request->getParam('id');
回答by soju
You should read this :
你应该读这个:
- http://www.yiiframework.com/doc/guide/1.1/en/topics.url
- http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action-parameter-binding
- http://www.yiiframework.com/doc/guide/1.1/en/topics.url
- http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action-parameter-binding
About your problem, you should simply create a corresponding url rule, e.g. :
关于您的问题,您应该简单地创建一个相应的 url 规则,例如:
'events/<param1>/<param2>'=>'events/view',
And then in your controller :
然后在您的控制器中:
public function actionView($param1, $param2)
{
// you can now use $param1 and $param2
}
回答by user2677802
You just need uri component. Here is the solution , you can use uri component like this ; Yii::app()->uri->segment(2);
您只需要 uri 组件。这是解决方案,您可以像这样使用 uri 组件;Yii::app()->uri->segment(2);
for details follow url
详情请关注网址
http://www.hasandemir.com/how-to-get-contoller-action-segments-like-codeigniter/
http://www.hasandemir.com/how-to-get-contoller-action-segments-like-codeigniter/

