0
votes

When I create this method

actionTest($parameter){
    print($parameter);
}

inside of a yii controller and attempt to access it in the browser I get an error 400.

How can I set up yii so that if I enter: /controller/test/text it would simply print the string 'text' on the screen instead of returning an invalid request error?

I already verified that the URL is correct. If I write

actionTest(){
  print('text'); 
} 

and then go to /controller/test/text then it works just fine.

How can I set up yii so that a controller action can accept parameter values in the URL?

3

3 Answers

3
votes

You have to edit the urlManager rewrite rules array in your config.php to include

'urlManager'=>array(
    ....
    'rules'=>array(
        'controller/test/<parameter:\w+>' => 'controller/test',
        ...
    ),
),

with your controller function as

actionTest($parameter){
   print($parameter);
}
1
votes

@curtis: Thanks. That helped!!! <controller:\w+>/<action:\w+>/<id:\w+>'=>'<controller>/<action> – Curtis Colly Mar 31 '13 at 3:27

The rule <controller:\w+>/<action:\w+>/<id:\w+>'=>'<controller>/<action> line will send the last parameter to the controller as $id if you want to use $parameter you should change id to parameter.

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

In the controller you can also type

actionTest(){
    print($_GET['parameter']); //or $_REQUEST
}
0
votes

Try the url: /controller/test/parameter/text , it should work. Suggested url rule by @topher is also correct.