mithila
(Mithila)
February 10, 2012, 5:34am
1
Hi all…
I am trying to pass a string to ma action instead of an ID. but when i do that i get 400 ERROR.
here is my sample code…
public function actionAll($category)
{
echo $category;
}
which is in members controller…
so my url is http://192.168.7.110/em/Members/All/Artist
as i said i get "Error 400 Your request is invalid." error.
I have referred to
Action Parameter Binding
Since version 1.1.4, Yii has added support for automatic action parameter binding. That is, a controller action method can define named parameters whose value will be automatically populated from $_GET by Yii.
To illustrate how this works, let’s assume we need to write a create action for PostController. The action requires two parameters:
category: an integer indicating the category ID under which the new post will be created;
language: a string indicating the language code that the new post will be in.
We may end up with the following boring code for the purpose of retrieving the needed parameter values from $_GET:
class PostController extends CController
{
public function actionCreate()
{
if(isset($_GET['category']))
$category=(int)$_GET['category'];
else
throw new CHttpException(404,'invalid request');
if(isset($_GET['language']))
$language=$_GET['language'];
else
$language='en';
// ... fun code starts here ...
}
}
Now using the action parameter feature, we can achieve our task more pleasantly:
class PostController extends CController
{
public function actionCreate($category, $language='en')
{
$category=(int)$category;
// ... fun code starts here ...
}
}
but dint achieve it… only $id works… Please help…
scspl.pravin
(Phpwebdeveloper900)
February 10, 2012, 5:37am
2
hi
can you submit view file code ?
Thanks
redguy
(Maciej Lizewski)
February 10, 2012, 7:09am
3
This is not an view issue.
You are trying to pass text GET parameter in path style. This is possible, however default routing rules are matching only numeric "id" parameter.
In order to make your example working you have to add routing in your main.php config like this:
'<controller:\w+>/<cact:\w+>/<action:\w+>'=>'<controller>/<cact>',
You should also always use createUrl method - it will generate proper url no matter if you have routing rules defined or not (if not - the url will have form like this: http://192.168.7.110/em/Members/All?action=Artist .
After few minutes one more think comes to my mind - your parameter name “action” can be in conflict with default routing rule where ‘action’ actually points to controllers action. If above solution won’t work, try to change your param name.
mithila
(Mithila)
February 12, 2012, 6:20am
4
@redguy … Well that worked for me… Thanks:) i should have checked it first only:)