Need Help On How To Pass Multiple Parameters To An Action


(Mbcmexico) #1

Hi there,

could anybody give some advice on how to configure the action to accept multiple parameters? let’s say, the URL is something like this:

www.domain.com/index.php/FooController/SomeAction/params

where params could be something like

cat/id

or

cat=3/id=5

or

cat=5&id=6&foo=something

I realized that my actual configuration only accepts one numeric parameter to be handled by

public function actionFoo ($int)

{

}

I appreciate your help


(Mwerlberger85) #2

Found it in this comment of the controller section (action parameter binding) of the guide:




// $int is needed

// $cat is optional with default value

// $foo is optional with no default value

public function actionFoo ($int, $cat='default value if you need it', $foo=null)

{

...

}




(Mbcmexico) #3

Oks,

but, how you build the url so the function accepts both paramaters?

i tried

/index.php/NameController/Foo/3/4

and it dont works

many thanks for your help


(Chellamnivas) #4

Dear Friend

You have to create url in the following way




Yii::app()->createUrl("fooController/someAction",array("int"=>"someNumber","cat"=>"someString","foo"=>"something"));



Regards


(Mbcmexico) #5

I will try this

many thanks for your help


(Mbcmexico) #6

Dear friend,

after chritsmas holidays we’re now working.

Could you please tellme whats wrong on this:

public function actionFoo ($int, $cmf=‘0’)

{

}

I build the link using

Yii::app()->createUrl("fooController/foo",array("int"=>"1","cfm"=>"8"));

then the builded link is

http://.../fooController/foo?int=1&cfm=%228%22

when you click on this link you receive this error from yii

Error 400

Your request is invalid.

Could you please help me on what im doing wrong?

thanks in advance


(Joe) #7
  • do you use the controllerId (foo not fooController)?



     //wrong: Yii::app()->createUrl("fooController/foo",array("int"=>"1","cfm"=>"8")); 

     Yii::app()->createUrl("foo/foo",array("int"=>"1","cfm"=>"8"));



  • you should find more information in the application.log file.

  • debug the request.


(Chellamnivas) #8

Dear Friend

Let us consider an example.

We have one controller TrialController.php in controller folder.

Then we have two views one.php and two.php in views/trial folder.

TrialController.php




<?php

class TrialController extends Controller

{

	

       // action one renders view one and displays the link.

	public function actionOne()

        {

		$this->render("one");

	}   


	public function actionTwo($id,$name)

       { 

             //$id and $name are available inside the method.You can do whatever logic you can do with them

             //Here we are just passing them to the view.	

	        $this->render("two",array("id"=>$id,"name"=>$name));

	}      

}




views/trial/one.php




<?php

$id=1;

$name="someValue";


//You can also pass dynamic variable from form submission or database tables.


echo CHtml::link("click",array("trial/two","id"=>$id,"name"=>$name));


//Here CHtml::link calls CHtml::normalizeUrl and creates url from the second parameter.

?>



views/trial/two.php




<?php

echo "your id is :".$id;

echo "</br>";

echo "your name is :".$name;

?>



I hope I helped a bit.

Regards.


(Rukushimei) #9

can you guys help me of how to set view to another view model? and configuring the controller? dunno how to set up things like that, newbie here, as in totally newbie :) thanks anyway