400 error when I click to add user Chapter 8

Hi I got up to page 210 when I’m supposed to click to add a new user in a project and see a form to add a user.

As far as I know, my code followed the code from the book, except in the Project Controller class I had to add the $id variable to addUser function, perhaps due to a change in versions.

Anyways, I know generally what a 400 error means, but I’m not getting any particular details from my application, even though I have error messages turned on.

Can anyone suggest how I might be able to fix this problem?

You are getting a 400, not a 403?

Could you post the exact url that is being attempted?

Also, you can download the trackstar source code (not more nor less completed than what is in the book) here:

to compare.

Yes, as you can see in the attached image, it’s a 400 error. I’m trying to access this URL


http://localhost:8888/demo/index.php/project/adduser

You can see the URLs in the various images. Note, I downloaded the code from the book and actually was using it (i.e. copying and pasting it into the application so I didn’t have to type it)

Does it mean that the signature of addUser action is the following:




public function actionAddUser($id)



?

If so, url must contain a parameter named "id", otherwise you get the 400 error.

This seems likely to be the case. It does appear you are not following along exactly with the book, which is okay, but there will be differences to deal with. If you were using version 1.1.2 and following strictly with the book, the url to access adding a new user to a project is of the form (assuming project id = 1 and domain name of trackstar):

http://trackstar/index.php?r=project/adduser&id=1

So you are missing the input identifier. Eliminating this will result in the following code being executed (in the ProjectController::loadModel() method):




throw new CHttpException(404,'The requested page does not exist.'); 

True, but not the case here. Execution doesn’t reach loadModel() method. Yii uses some Reflection magic to check if the action method has any required parameter. If it has, and the parameter can’t be found in URL, action won’t run and an error 400 will be thrown.

I’m sorry, I get the general idea, but I don’t fully understand. Is there something I can do to make the URL work as intended?

Add project id as "id" parameter to the URL when generating "Add User to Project" link.




$this->createUrl('addUser', array( 'id' => $project->id))



I’m trying to incorporate your suggestion but I can’t get it to work

this is the original URL in project/view.php


$this->menu[] = array('label'=>'Add User To Project', 'url'=>array('addUser'));

Based on what you wrote in your last post, and because my link was a little different, I tried to do


$this->menu[] = array('label'=>'Add User To Project', 'url'=>array('addUser', array( 'id' => $project->id)));



and


$this->menu[] = array('label'=>'Add User To Project', 'url'=>array('addUser', 'id' => $project->id ));

but both of them broke my project

Any suggestions?

Oops, sorry, the exmple code in the book uses "$model" variable instead of "$project".




$this->menu[] = array('label' => 'Add User To Project', 'url' => array('addUser', 'id' => $model->id));