Scaffold CRUD with Yii 2 Gii – Step‑by‑Step Guide
Generate a working model, controller and views for a database table using Yii 2’s Gii tool, then verify the CRUD UI and know how to roll back changes.
03 Feb 2026, 08:35 UTC

Desired outcome
You want a ready‑to‑use set of files—a model that extends yii\db\ActiveRecord, a controller with the standard actions (index, view, create, update, delete), and a view folder containing the corresponding PHP scripts—so that navigating to URLs like /post/index presents a functional CRUD interface for the chosen database table.
Prerequisites
- A Yii 2.x application (basic or advanced template) that is already running on a web server.
- A database connection defined in
config/db.php(orconfig/main.phpfor the advanced template) and the target table existing in that database. - The Gii module enabled in the application configuration. In
config/web.php(basic) orconfig/main-web.php(advanced) you should have:
'modules' => [
'gii' => [
'class' => 'yii\gii\Module',
// optional: restrict access to specific IPs in production
'allowedIPs' => ['127.0.0.1', '::1', 'YOUR_SERVER_IP'],
],
],
@app/models, @app/controllers, and @app/views (or their namespace‑specific equivalents).Focused procedure
- Open your browser and navigate to the Gii entry point, e.g.
http://your-host/gii. You should see the Gii dashboard. - Click the CRUD Generator icon.
- In the form:
- Table Name: enter the exact name of the database table you want to scaffold (e.g.,
post). - Model Class: optionally adjust the class name; Gii will suggest a CamelCase version based on the table name (e.g.,
Post). - Controller ID: optionally set the controller identifier used in URLs (default is the lowercase model class, e.g.,
post).
- Table Name: enter the exact name of the database table you want to scaffold (e.g.,
- Press Preview. Gii will list the files it intends to create or overwrite, showing their full paths.
- Review the list. If you are using version control, ensure the working directory is clean or that you have a recent commit.
- Click Generate. Gii writes the files to the appropriate directories.
Expected checks
- File system verification: run the following command in the root of your Yii project (replace
postwith your table/model name if different):
You should see three entries: a model file, a controller file, and a directory containingls -l @app/models/Post.php @app/controllers/PostController.php @app/views/post/index.php,view.php,create.php,update.php, and_form.php. - Class inspection: open
@app/models/Post.phpand confirm it begins with something like:
and that it contains anamespace app\models; use yii\db\ActiveRecord; class Post extends ActiveRecord { // … }rules()method with validation rules for each column. - Controller actions: in
@app/controllers/PostController.phpverify the presence of:public function actionIndex() { … } public function actionView($id) { … } public function actionCreate() { … } public function actionUpdate($id) { … } public function actionDelete($id) { … } - Browser test: navigate to
http://your-host/post/index(or/postif you enabled pretty URLs). You should see a table listing existing rows, with buttons for “Create Post”, “Update”, “View”, and “Delete”. Clicking each should produce the expected form or confirmation without PHP errors.
Limitations and practical verification
- Gii will overwrite any existing file that matches the generated name without prompting. If you have customized a model or controller, those changes will be lost.
- The generator uses the default application namespace (
app) unless you have edited the configuration. If your project uses a custom namespace (e.g.,frontend\modelsin the advanced template), you must adjust the Model Class field accordingly, otherwise you will encounter “class not found” errors. - Generated code relies on the database schema at the moment of generation. If you later alter the table (add/drop columns), the model’s validation rules and attribute labels will become out‑of‑sync.
To verify that the generated CRUD works as expected after any schema change, repeat the Gii process (or manually update the model) and run the browser test again.
Recovery / rollback options
Because file generation modifies the repository state, you should have a rollback strategy ready:
- Version control: if you are using Git, commit or stash your work before running Gii. After generation, if the output is unsatisfactory, run:
or simply reset the commit withgit checkout -- @app/models/Post.php @app/controllers/PostController.php @app/views/post/git reset --hard HEAD. - Manual backup: copy the target directories to a temporary location (e.g.,
cp -r @app/models @app/models.backup) before generating. To restore, delete the newly created files and copy the backup back. - Selective undo: if only one file is problematic, you can delete or edit that specific file and re‑run the Gii generator for the same table; Gii will overwrite it again with the default version.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.