How to get $_GET as action parameters

Hi @ all !

I’m trying to set a parameter to an action in my controller and get it via GET

example:

localhost/default/my_action/name/foobar

Then I’m trying to use name/foobar as params of my action like this:




public function actionmy_action($name) {

echo $name;

}

Here I expect a page that says ‘foobar’ but i get :


Missing argument 1 for defaultController::actionMy_action(), called in C:\wamp\framework\web\actions etc....

How I can solve this ?

Do not pass parameters to actions. just access $_GET[‘arrayIndex’] in your actions…

You should also specify url rules to maths the url u desire. eg:


'index/<name:\w+>'=>array('site/index')

and you enter the app though domain.tld/index/MyName

The value MyName is available in your actions etc… through $_GET[‘name’]

I m trying to do a multi-step form.

this is why i need to relaunch my controller action with a defined step as parameter if an error occurs.

example:


public function actionmy_action($step) {

switch ($step):

  case 1:

    //(the first step of my form)

  case 2:

    //(the 2nd step of my form)

    if ($model->hasErrors()) #if an error is found here

      {

        // back to step 1

        $this->actionmy_action(1);

      }

endswitch;

I think there is a bug from the PHP interpreter because it gets a syntax error with :


public function actionmy_action($_GET['step']=1) {}

result:


Parse error: parse error, expecting `')'' in C:\wamp\www etc....

you still do not pass a parameter to your action. redirect to the step you desire with


$this->redirect(array('controller/action','step'=>'stepNumber'));

from your action.

It is not a part of the convention of Yii to pass values to your actions through parameters.

Ok man,

I asked for this because I wanted to avoid using of redirect(), because this sends new redirection headers so the browser will have to reask for a new ‘controller/action/step’ (HTTP GET /controller/action/step )

Refer the sample in Yii manual

http://www.yiiframework.com/doc/guide/form.action

You can see how to access the GET or POST variable in action method.

Well, instead of using a redirect, you can create the form’s action attribute to have the proper $_GET items




<form action="<?php echo $this->createUrl(array('controller/action','step'=>'stepNumber')); ?>" method="post">

  ... inputs ...

</form>



Then you can structure your action like this:




public function actionMy_Action()

{

  $step = (isset($_GET['step']) ? $_GET['step'] : 1);

  switch ($step)

  {

  ....

  }

}



This syntax:




public function actionmy_action($_GET['step']=1) {}



is wrong. Firstly, with Yii action functions do not have parameters passed to them. Secondly, you should not (and cannot) assign default values to super global variables like this.

Thanks,

but I already made a similar scenario :




$s = Yii::app()->request->getQuery('step',1);

switch($s):

  case 1:

  $this->render('step1',array('model'=>$model);

  break;

  case 2:

  // etc....

  break;

endswitch;