How to get Yii parameters directly from the querystring (URL)

33,126

Solution 1

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

Solution 2

you can try getParam method

$id = Yii::app()->request->getParam('id');

Solution 3

You should read this :

About your problem, you should simply create a corresponding url rule, e.g. :

'events/<param1>/<param2>'=>'events/view',

And then in your controller :

public function actionView($param1, $param2)
{
    // you can now use $param1 and $param2
}
Share:
33,126
Zabs
Author by

Zabs

PHP Developer

Updated on July 05, 2022

Comments

  • Zabs
    Zabs almost 2 years

    I am using the latest version of Yii and trying to basically collate the URL parameters.

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

    I remember CodeIgniter has $this->url->segment(1), I basically need the same functionality to Yii :)

  • Zabs
    Zabs over 10 years
    I will do have to wait a few more mins :)
  • iltaf khalid
    iltaf khalid almost 10 years
    I had the follwing URL i.e. localhost/FedSeed/reports/multi/1 and I used CHttpRequest::getParam('id'); to access the last 1 digit :)