rockstardev
rockstardev

Reputation: 13527

Accessing paramters from the request string in YII?

Let's say I have the following view:

http://localhost/site/www/index.php/products/view/1

Then

Yii::app()->request->getUrl() ==> /site/www/index.php/products/view/1 
Yii::app()->getController()->id ==> products
Yii::app()->getController()->getAction()->id; ==> view 

How do I access the "/1" part?

Upvotes: 0

Views: 665

Answers (2)

darkheir
darkheir

Reputation: 8950

There are two ways to get the id values. Let's say you defined the following rule for the Url:

'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

Then you can get the value of ID by using GET method in php:

$id = $_GET['id'];

Or u can define in your controller a param for the method, the param will automatically be the id u need:

public function viewAction($id) {
    //here $id is equal to $_GET['id']
}

Be careful, the name of these parameters must be exactly the same as the ones we expect from $_GET

Upvotes: 2

Brett Gregson
Brett Gregson

Reputation: 5913

You can access it as a $_GET variable using:

$_GET['id']

How you ask? Because of the rules set up in the Yii default config (protected/config/main.php)

'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

That rule specifies a {controller}/{action}/{a value, named id}. You can customize these rules to whatever you want, read more about it here:

Upvotes: 1

Related Questions