Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Yii2 Gii Laravel Package

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.

Deep Wiki
Context7

Getting Started

Minimal Steps to First Use

  1. 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.)

  2. Basic Setup (Yii2 Context)

    • Configure gii in config/web.php (Yii2):
      'modules' => [
          'gii' => [
              'class' => 'yii\gii\Module',
              'allowedIPs' => ['127.0.0.1', '::1'], // Restrict access
          ],
      ],
      
    • Enable Gii URL mapping in config/web.php:
      'urlManager' => [
          'enablePrettyUrl' => true,
          'rules' => [
              'gii' => 'gii',
          ],
      ],
      
  3. First Use Case: Generate a CRUD Controller

    • Navigate to /gii/crud in your Yii2 app.
    • Select a model (e.g., User).
    • Customize fields (e.g., exclude password).
    • Generate and test the CRUD scaffolding.

Laravel Workaround (If Needed)

Since Gii is Yii2-specific, for Laravel:

  • Use Laravel Generators (e.g., laravel-shift/generator) or Artisan commands for scaffolding.
  • Example: Generate a model + controller via CLI:
    php artisan make:model User -m --controller=UserController
    

Implementation Patterns

Common Workflows

  1. Model-Based CRUD Generation

    • Use Gii to auto-generate controllers, views, and migrations for a model.
    • Customize templates (e.g., override crud/index.php) in vendor/yiisoft/yii2-gii/views/crud/.
  2. API Resource Scaffolding

    • Generate API controllers with Gii’s rest generator.
    • Example: /gii/rest?modelClass=User&ns=api\v1.
  3. Form Builder Integration

    • Use Gii’s model generator to create forms with ActiveForm (Yii2).
    • For Laravel, replace with Collective HTML or Laravel Nova.
  4. Database-Driven Development

    • Reverse-engineer models from an existing database via Gii’s model generator.
    • Map tables to classes with custom namespace rules.

Integration Tips

  • 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'],
    ],
    

Gotchas and Tips

Pitfalls

  1. Yii2 vs. Laravel Incompatibility

    • Gii will not work natively in Laravel. Treat it as a Yii2 tool.
    • Workaround: Use it for Yii2 microservices or admin panels within a Laravel app.
  2. Database Schema Assumptions

    • Gii assumes conventional table/column names (e.g., id, created_at).
    • Fix: Customize the model generator’s tablePrefix or use dbColumnTypes.
  3. Template Overrides Not Loading

    • Ensure custom templates are placed in the correct path (views/gii/).
    • Debug: Clear Yii2’s cache (php yii cache/flush).
  4. CSRF Token Mismatches

    • Gii forms may fail if Laravel’s CSRF middleware is active.
    • Fix: Exclude Gii routes from middleware in app/Http/Kernel.php:
      protected $except = [
          'admin/gii/*',
      ];
      
  5. Namespace Conflicts

    • Gii-generated classes may clash with Laravel’s autoloader.
    • Solution: Use custom namespaces in Gii’s modelClass parameter:
      // Example: Generate in \App\Admin\Models\
      'modelClass' => 'App\Admin\Models\User',
      

Debugging Tips

  • 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:

    • The table exists in the database.
    • The db component is configured in config/db.php (Yii2).

Extension Points

  1. 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',
        ],
    ],
    
  2. 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);
    }
    
  3. 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)
        }
    );
    

Performance Quirks

  • 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
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport