Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :)

like image 361
Zabs Avatar asked Jan 23 '14 13:01

Zabs


People also ask

How do I redirect a URL in Yii?

Yii::app()-request->redirect('/path/to/url');

What is Querystring in URL?

A query string is the portion of a URL where data is passed to a web application and/or back-end database. The reason we need query strings is that the HTTP protocol is stateless by design. For a website to be anything more than a brochure, you need to maintain state (store data).

What is get parameter in URL?

GET parameters (also called URL parameters or query strings) are used when a client, such as a browser, requests a particular resource from a web server using the HTTP protocol. These parameters are usually name-value pairs, separated by an equals sign = . They can be used for a variety of things, as explained below.


1 Answers

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

like image 131
Rohan Avatar answered Oct 13 '22 20:10

Rohan