Accessing Urlrules Pattern Parameters

Hi All,

I have an REST API implemented using YII framework and this framework URL structured is constructed to access based on country.

Here is my URLRule looks like

array('<module>/default/readCollection',       'pattern' => '<country:\w+>/<language:\w+>/<module:\w+>',                                                                        'verb' => 'GET'),

It looks like

From the above URL us=country, en=language

I would like to access those two params across the API (I mean, in all controllers and models)

Currently I’m access the country and language names by following way



My question is that I don’t want to access them using request->getParam(‘country’) in my models and controllers, I want these two parameters to be Application parameters

Example, I would like to access Yii::app()->country Yii::app()->language

How could I make those to request parameters app level vairalbes :-).

would appreciate your help

Add a behavior to your application:

Create a file ApiBehavior.php in protected/components.

But Yii::app()->language is reserved by Yii.

I would call the behavior methods ‘apiCountry’ and ‘apiLanguage’.

class ApiBehavior extends CBehavior


    public function getApiCountry()


       return $this->getOwner()->request->getParam('country'); //or Yii::app()->request->getParam('country');


    public function getApiCountry()


       return $this->getOwner()->request->getParam('language'); //or Yii::app()->request->getParam('language');


Add this component to the application behaviors in config/main.php

return array(


    'behaviors' => array(        

        'apibehavior' => 'ApiBehavior',



   'modules' => array( ... ),

   'components' => array(....), 

Now you should be able to call:

Yii::app()->getApiCountry() or Yii::app()->apiCountry??

Yii::app()->getApiLanguage() or Yii::app()->apiLanguage??

EDIT: Not shure if Yii::app()->apiCountry,->apiLanguage will work.

Hi Jablo,

Thank you very much, that was a nice Insight, really appreciate your help :-).