Passing Variable By Url In Yii?

I know how to use POST and GET method in yii, now I want to pass the data by URL link. For example, I want the output like this: “site/index.php?var1=data1&var2=data2” and retrieve the data with $_POST[‘var1’]. Is there any specific way to do these actions in Yii or just simply use the normal php way?

You can just use $_GET[‘blah’] to retrieve the value. Alternative, you can structure your controller action as follows:


public function actionIndex($uid = '') {

    // $uid will contain the uid data variable from the url site/index?uid=2 

}

it is recommended to avoid accessing _GET, _POST and _REQUEST arrays directly. you should use




Yii::app()->request->getParam( 'name', 'default value' );

Yii::app()->request->getPost( 'name', 'default' );



or bind attributes to action function params as clonevm wrote.

Why is it recommended to avoid?


        public function getQuery($name,$defaultValue=null)

        {

                return isset($_GET[$name]) ? $_GET[$name] : $defaultValue;

        }



If you don’t need a default value you’re just pulling the variable from $_GET anyway.


        public function getQuery($name,$defaultValue=null)

        {

                return isset($_GET[$name]) ? $_GET[$name] : $defaultValue;

        }

These code above must be put into my controller or just use this?

One more thing is about the direction, for example, I currently open the page site/index.php in the folder site and want to perform an action to link to other page main/action.php in other folder with these code "main/action.php?back1=data1&back2=data2". Im trying to use both redirect and header to do it, not yet use form to send data. The problem is either redirect or header, the root path is from C: drive, not inside the framework folder. So it turn to become like that: redirect("C:/xampp/httdocs/yii/main/action.php?back1=data1&back2=data2"). Is there any way to do it more convenient with Yii style (without using form)?

because it is not a good design pattern… just because those tables are still accessible does not mean it is good practice to use them.

redirectoing to another action usually is not needed. you can use CAction descendant actions or other components/helpers/etc that should allow you get same effect without redirects… even between applications - you can share components/models folder, etc. if there are common code parts - they should be moved to another class which can be used in every place where same logic is needed.

Thank for your reply, but as a newbie, I don’t understand what you described :(.

read this about creating actions as classes and reusing them in different controllers: http://www.yiiframework.com/wiki/170/actions-code-reuse-with-caction/