Yii Framework Forum: Passing Variable By Url In Yii? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Passing Variable By Url In Yii? Rate Topic: -----

#1 User is offline   clonevn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-March 13

Posted 28 April 2013 - 05:29 AM

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?
0

#2 User is offline   Artificial 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 11-December 10
  • Location:Sydney, Australia

Posted 28 April 2013 - 05:45 AM

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 
}

0

#3 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 773
  • Joined: 02-July 10
  • Location:Central Poland

Posted 28 April 2013 - 06:16 AM

View Postclonevn, on 28 April 2013 - 05:29 AM, said:

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?

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.
red
0

#4 User is offline   Artificial 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 11-December 10
  • Location:Sydney, Australia

Posted 28 April 2013 - 06:24 AM

View Postredguy, on 28 April 2013 - 06:16 AM, said:

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

#5 User is offline   clonevn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-March 13

Posted 28 April 2013 - 07:06 AM

        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)?
0

#6 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 773
  • Joined: 02-July 10
  • Location:Central Poland

Posted 28 April 2013 - 03:41 PM

View PostArtificial, on 28 April 2013 - 06:24 AM, said:

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.

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.
red
0

#7 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 773
  • Joined: 02-July 10
  • Location:Central Poland

Posted 28 April 2013 - 03:46 PM

View Postclonevn, on 28 April 2013 - 07:06 AM, said:

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

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.
red
0

#8 User is offline   clonevn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-March 13

Posted 28 April 2013 - 10:52 PM

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

#9 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 773
  • Joined: 02-July 10
  • Location:Central Poland

Posted 29 April 2013 - 03:36 AM

View Postclonevn, on 28 April 2013 - 10:52 PM, said:

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.yiiframew...e-with-caction/
red
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users