Error 400

Hi,

I’m pretty new to Yii, so this might be a silly question. I must be missing something somewhere. Plz help me out.

I have just written a simple code while I’m learning Yii.

I have a user controller which has an action that looks like this:




	public function actionGetUsername($userId)

	{

		$user = Users::model()->findByPK($userId);

		$username = $user->username;

		$this->render('index', array('username'=>$username));

	}



Now I have enabled SEO friendly URLs and echo statement in the view file to display the returned username.

But when I go to the URL, sitename/user/getusername/1

I get error 400 - your request is invalid.

Plz let me know if I am missing something here.

Hi ,

You can try like ,

pass URL like : sitename/user/getusername&id=1


public function actionGetUsername()

	{

                $userId = $_REQUEST['id'];

		$user = Users::model()->findByPK($userId);

		$username = $user->username;

		$this->render('index', array('username'=>$username));

	}

it will work.

Hi ,

You can try like ,

pass URL like : sitename/user/getusername&id=1


public function actionGetUsername()

	{

                $userId = $_REQUEST['id'];

		$user = Users::model()->findByPK($userId);

		$username = $user->username;

		$this->render('index', array('username'=>$username));

	}

it will work.

Hi,

Thanks for that. But I would like the SEO friendly URLs to work.

There must be some workaround to this right?

I was also trying to write a function to display user info when the path has the username, like:

sitename/user/username

That is why I need to get that parameter value without using &username=somename

Do I have to do anything with the URL rules?

Plz let me know

Parameter names in action methods must match the parameter names in the corresponding urlManager rule.

For


public function actionGetUsername($userId)

you should have something like


/<userId:\d+>

in the rule.

guide - action parameter binding

Did you add the getUsername to accessRules() in the controller ?




public function accessRules()

{

   return array(

            array('allow',

                      'actions=>array('index', 'view', 'getUsername'),

                      'users'=>array('*'),

            ..............

}



Also, have a look @ http://www.yiiframework.com/doc/guide/1.1/en/topics.url

It talks about URL management in Yii