In this article you’ll learn how to create CRUD in yii2 using GII.
This below is sample output :
Let’s do it !
CONNECTION
First, configure the database connection in common/config/main-local.php :
TABLE
GENERATE CRUD USING GII
Access GII on :
http://localhost/your_yii/frontend/web/index.php?r=gii
And will be display like this :
1) First, we must create a model.
Click a button ‘start’ at the Model Generator. Then fill the input ‘Table name’ below with ‘book’ :
Click Preview and then click Generate.
2) Next, click ‘CRUD Generator’ at the top left menu. Then fill the input like below
Click Preview and then click generate.
3) Finish!!! You can try the CRUD by URL below :
http://localhost/your_yii/frontend/web/index.php?r=book
if you get error like this :
Unable to find ‘frontend\controllers\BookController’ in file: /var/www/html/yii2_advanced/frontend/controllers/BookController.php. Namespace missing?
Open frontend/controllers/BookController.php, find :
namespace app\controllers
change it to
namespace frontend\controllers