yii2 and Bad Request (#400) with Missing required parameters: ID

Author:

Using Yii 2 is really easy, but for a newbie sometimes the Gii (i.e. the code generator helper tool) may generate a code, which only the index page works and the other pages like view, update generates a strange error of missing ID (note it is capitalized ID):


Bad Request (#400)
Missing required parameters: ID

The above error occurred while the Web server was processing your request.

Please contact us if you think this is a server error. Thank you.

SCREENSHOT 1) Just viewing an item generates the error of a bad request.

main menu
view with id

Here is the tracing log:

error	yii\web\HttpException:400	yii\web\BadRequestHttpException: Missing required parameters: ID in /home/myuser/projects/test-web/vendor/yiisoft/yii2/web/Controller.php:202
Stack trace:
#0 /home/myuser/projects/test-web/vendor/yiisoft/yii2/base/InlineAction.php(51): yii\web\Controller->bindActionParams()
#1 /home/myuser/projects/test-web/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams()
#2 /home/myuser/projects/test-web/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction()
#3 /home/myuser/projects/test-web/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction()
#4 /home/myuser/projects/test-web/vendor/yiisoft/yii2/base/Application.php(384): yii\web\Application->handleRequest()
#5 /home/myuser/projects/test-web/web/index.php(12): yii\base\Application->run()
#6 {main}

Probably, the problem is in the Controller class of the route you are trying to view. For example, the above URL is http://localhost:8888/odd-type/view?id=1 the route is /odd-type/view and apparently, the OddTypeController class may not find the ID parameter.
SCREENSHOT 2) The function actionView has the parameter $ID, which is used in the function findModel($ID).

main menu
actionView with capitalized ID


SCREENSHOT 3) There are three more functions actionUpdate, actionDelete and the third is below.
main menu
actionUpdate and actionDelete with capitalized ID

SCREENSHOT 4) The last function with capitalized $ID, which must be $id.
main menu
findModel with capitalized ID

The solution is to open the controller class of the route, which in this case is OddTypeController, and make all function parameters from capitalized $ID to $id. Save the file and it won’t give this error anymore.
SCREENSHOT 5) Replace the two occurrences of $ID with $id as shown. Do it for all of the above functions – actionView, actionUpdate, actionDelete, and findModel.

main menu
actionView with id

The capitalized $ID must be replaced with $id in all of the functions – actionView, actionUpdate, actionDelete, and findModel. Note, the parameter variable $id is used in the functions’ bodies, so replace it with the correct one in the functions’ bodies, too.

Yii 2 Debug Toolbar tracing the error to the Controller class.

Here is a screenshot with an open debug toolbar, which shows a trace of the error.

main menu
Yii 2 Debug Toolbar Controller error

Leave a Reply

Your email address will not be published. Required fields are marked *