Creating Controller and views using Gii tool in Yii2

Posted by in Yii 2

CRUD Generator

This generator generates a controller and views that implement CRUD (Create, Read, Update, Delete) operations for the specified data model.

Click “start” button under CRUD generator to create Controller and Views for corresponding Model.

Model Class

Here you should provide the fully qualified class name of already created Model.
eg: frontend\models\Products
Note : If your model was generated in backend, put backend instead of frontend. eg: backend\models\Products

Search Model Class

Here you can provide search model class.
eg: frontend\models\ProductsSearch

Controller Class

Here you can provide controller class to be generated.
eg: frontend\controllers\ProductsController

Creating Controller & Views using Gii tool

Creating Controller & Views using Gii tool

Click on the “Preview” button to view the file path and then click “Generate” button to generate files.
possible error: Class 'frontend\models\Products' does not exist or has syntax error.
solution: Replace namespace app\models to namespace frontend\models or namespace backend\models
Now your controller file is generated in @app\frontend\controllers and view files are generated in @app\frontend\views
File structure will be like this

you can access create form through this link
eg: http://localhost/app/frontend/web/index.php?r=products/create
you can access index page through this link
eg: http://localhost/app/frontend/web/index.php?r=products/index
* app – your app name.
*products – your controller name.

Share your comments