yiisoft/yii2-gii
Gii is Yii2’s web-based code generator. Rapidly scaffold models, CRUD, controllers, forms, and modules with templates you can customize. Speeds up development and enforces consistent structure, with an extensible generator system for your own blueprints.
Installation Add the package via Composer in your Laravel project (though note: Gii is Yii2-specific, not native to Laravel):
composer require yiisoft/yii2-gii
(Note: This package is primarily for Yii2, not Laravel. For Laravel, consider alternatives like Laravel IDE Helper, Laravel Generator, or custom Artisan commands.)
Basic Setup (Yii2 Context)
gii in config/web.php (Yii2):
'modules' => [
'gii' => [
'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1', '::1'], // Restrict access
],
],
config/web.php:
'urlManager' => [
'enablePrettyUrl' => true,
'rules' => [
'gii' => 'gii',
],
],
First Use Case: Generate a CRUD Controller
/gii/crud in your Yii2 app.User).password).Since Gii is Yii2-specific, for Laravel:
laravel-shift/generator) or Artisan commands for scaffolding.php artisan make:model User -m --controller=UserController
Model-Based CRUD Generation
crud/index.php) in vendor/yiisoft/yii2-gii/views/crud/.API Resource Scaffolding
rest generator./gii/rest?modelClass=User&ns=api\v1.Form Builder Integration
model generator to create forms with ActiveForm (Yii2).Database-Driven Development
model generator.Yii2 + Laravel Hybrid? If mixing stacks, use Gii for Yii2 modules (e.g., admin panels) and Laravel for the frontend. Example:
// routes/web.php (Laravel)
Route::prefix('admin')->group(function () {
Route::get('gii/{path}', '\yii\web\Application::handleRequest');
});
Custom Templates
Override Gii templates in views/gii/ to match your app’s styling (e.g., Bootstrap 5).
Example structure:
resources/
├── views/
│ ├── gii/
│ │ ├── crud/
│ │ │ ├── index.php
│ │ │ └── ...
Authentication
Protect Gii with Yii2’s authManager or Laravel middleware:
// Yii2 config/web.php
'gii' => [
'class' => 'yii\gii\Module',
'allowedIPs' => ['192.168.1.100'],
'auth' => ['class' => 'yii\filters\AuthInterface'],
],
Yii2 vs. Laravel Incompatibility
Database Schema Assumptions
id, created_at).model generator’s tablePrefix or use dbColumnTypes.Template Overrides Not Loading
views/gii/).php yii cache/flush).CSRF Token Mismatches
app/Http/Kernel.php:
protected $except = [
'admin/gii/*',
];
Namespace Conflicts
modelClass parameter:
// Example: Generate in \App\Admin\Models\
'modelClass' => 'App\Admin\Models\User',
Enable Gii Debugging
Add to config/web.php:
'gii' => [
'debug' => true, // Logs generator actions
],
Check Generator Logs
Yii2 logs Gii actions to runtime/logs/gii.log.
Validate Model Binding If a model isn’t detected, verify:
db component is configured in config/db.php (Yii2).Custom Generators Extend Gii by creating a new generator class:
namespace app\gii\generators;
class MyGenerator extends \yii\gii\Generator {
public $template = '@app/gii/my-template';
}
Register it in config/web.php:
'gii' => [
'generators' => [
'mygen' => 'app\gii\generators\MyGenerator',
],
],
Dynamic Field Rules
Override getField in a custom generator to add logic:
public function getField($attribute) {
if ($attribute === 'password') {
return ['type' => 'password'];
}
return parent::getField($attribute);
}
Post-Generation Hooks
Use Yii2’s Event system to run code after generation:
\yii\base\Event::on(
\yii\gii\Generator::class,
\yii\base\Generator::EVENT_AFTER_GENERATE,
function ($event) {
// Post-processing (e.g., run migrations)
}
);
Large Database Tables
Gii may time out on tables with >100 columns.
Fix: Increase PHP’s max_execution_time or split the generator into chunks.
Template Compilation Gii templates are compiled on first run. Clear caches if changes don’t reflect:
php yii cache/flush
How can I help you explore Laravel packages today?