Controller Function Parameter Only $Id?

Hi All,

I’m curious, why is it so, that in a controller’s function (for example in actionAdmin()), a parameter is only allowed to be $id. I’ve tried to use $whatever, but I get Error 400 Your request is invalid. If I change it back to $id, everything is fine. Can somebody explain me why is that?

Thanks!

BR

c

when you define action params so yii look for params as ACtionname exactly what you defined and if your action params doesnt found error accured.

so if you change change $id => $whatever you should call $whatever not $id

no. I don’t know why but for me it’s not working with any other variable than $id.

Because it’s not a simple variable that’s passed to the action. The name of the action’s parameter and name of url parameter must be the same.




public function actionAdmin($test)

{

....

}


echo CHtml::link('Link Text', array('controller/admin', 'test' => '123')); // Cannot be 'id' => '123'



Matt

I was just typing it in the address bar, there was no link yet.

Maybe you should post the code of your actionAdmin?




    public function actionAdminTree($id) {

        $model = new Gyartmany('searchTree');

        $model->unsetAttributes();


        if (isset($_GET['Gyartmany']))

            $model->setAttributes($_GET['Gyartmany']);


        $this->render('adminTree', array(

            'model' => $model,

            'id' => $id,

        ));

    }



So you’re specifically specifying one argument that can be passed on to your action, and that argument is named [font=“Courier New”]$id[/font]. If you wanted to change the argument name in your urls, you have to change it as well in the action function declaration:


 public function actionAdminTree($whatever) { // Here

        $model = new Gyartmany('searchTree');

        $model->unsetAttributes();


        if (isset($_GET['Gyartmany']))

            $model->setAttributes($_GET['Gyartmany']);


        $this->render('adminTree', array(

            'model' => $model,

            'id' => $whatever, // Here as well, of course

        ));

    }

yes, I know, but still, if I put this:


 public function actionAdminTree($whatever) ... 

I get an Error 400 Your request is invalid.

Ok. You may have a url rule mapping that controller / action couple?

Also what’s the url you typed in the address bar?

I was typing: http://localhost/project/gyartmany/adminTree/1

ohh yes, you are right!




'urlManager' => array(

        'urlFormat' => 'path',

        'showScriptName' => false,

        'caseSensitive' => true,

        'rules' => array(

            '<controller:\w+>/<id:\d+>' => '<controller>/view',

            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',

            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',

        ),

    ),



that’s where the problem is. What canI do with it? I mean I know I can replace the specific line, or add a new line, but I don’t want to mess up everything. what is the good solution for this? at first I read through the guide.

One simple solution is to add before the first rule:


'rules' => array(

    'controller/action/<whatever:\d+>' => 'controller/action', 

    ...

)

See also http://www.yiiframework.com/doc/guide/1.1/en/topics.url