Hi! I have some controller like this
public function actionIndex($id){
$id = Yii::$app->request->get('id');
Advise me please how can i validate that ‘id’ is int or in_array(‘sale’,‘wait’) with yii2 tools
Hi! I have some controller like this
public function actionIndex($id){
$id = Yii::$app->request->get('id');
Advise me please how can i validate that ‘id’ is int or in_array(‘sale’,‘wait’) with yii2 tools
Do you mean that ‘id’ can be an integer or a fixed string that is ‘sale’ or ‘wait’?
Then I would write something like this:
public function actionIndex($id)
{
// $id = Yii::$app->request->get('id'); // no need to do this
if ($id == 'sale') {
...
} elseif ($id == 'wait') {
...
} elseif ($id > 0 ) {
...
}
}
You don’t have to read ‘id’ from GET parameter by yourself, because Yii will do it for you.
https://www.yiiframework.com/doc/guide/2.0/en/structure-controllers#action-parameters
Or, I would prefer a cleaner code:
public function actionIndex($id, $operation = null)
{
if ($operation == 'sale') {
...
} elseif ($operation == 'wait') {
...
} else {
...
}
}