To integrate ajaxGii into your Yii2 application and customize the Gii templates, follow the steps below:

Step 1: Install ajaxGii via Composer

First, install the ajaxGii extension via Composer:

composer require machour/yii2-ajaxgii

This package extends the default Gii generator to support AJAX-based CRUD generation.

Step 2: Configure ajaxGii in Yii2

Once the package is installed, you need to enable ajaxGii in your Gii module configuration.

  1. Edit the frontend/config/main-local.php or backend/config/main-local.php file (depending on where you want to enable Gii). Add the following to your configuration:
    if (YII_ENV_DEV) {
    // Configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
    'class' => 'yii\gii\Module',
    'generators' => [
    'crud' => [
    'class' => 'machour\yii2\ajaxgii\generators\crud\Generator', // ajaxGii CRUD generator
    ],
    ],
    ];
    }

This configuration replaces the default crud generator with ajaxGii‘s generator, which generates CRUD operations that use AJAX.

Step 3: Use ajaxGii

Now, access Gii in your browser (usually at http://localhost/yii2app/gii) and you will see the ajaxGii CRUD generator available.

  1. Go to the CRUD generator in Gii.
  2. Enter the model class and other necessary information, and generate the code.
  3. The generated CRUD will use AJAX-based actions for create, update, and delete.

Step 4: Customize Gii Templates

If you want to customize Gii templates, including those used by ajaxGii, you can do so by overriding the default template files.

1. Create a Custom Template Directory

Create a directory in your project where you want to store your custom Gii templates. For example:

@app/gii-templates/crud

2. Copy Existing Templates

You can copy the default Gii or ajaxGii templates from vendor into your custom template folder and modify them.

For example, copy the default CRUD templates from:

vendor/yiisoft/yii2-gii/src/generators/crud/default

or

vendor/machour/yii2-ajaxgii/generators/crud/default

into your custom folder:

@app/gii-templates/crud

3. Configure Gii to Use Your Custom Template

To tell Gii to use your custom templates, update the gii module configuration like this:

if (YII_ENV_DEV) {
// Configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'generators' => [
'crud' => [
'class' => 'machour\yii2\ajaxgii\generators\crud\Generator',
'templates' => [
'myCrudTemplate' => '@app/gii-templates/crud', // Specify your custom template path here
],
],
],
];
}

This tells Gii to use the @app/gii-templates/crud directory as the template source for generating CRUD code.

4. Modify the Custom Templates

You can now edit the files in @app/gii-templates/crud to customize the output. For example, you can modify:

  • controller.php: Customize the generated controller.
  • index.php, view.php, create.php, update.php: Customize the views.
  • search.php: Modify how the search model is generated.

These files will be used by Gii when generating CRUD code.

Step 5: Test the Custom Template

  1. Go to http://localhost/yii2app/gii again.
  2. Generate CRUD using the ajaxGii CRUD generator.
  3. Make sure to select your custom template (myCrudTemplate) from the “Code Template” dropdown (if it appears). If you set it as the default template in the configuration, it should be used automatically.

After generation, the generated files will reflect your customizations.


By following these steps, you will have successfully integrated ajaxGii into your Yii2 application, enabled AJAX-based CRUD operations, and customized the Gii templates to fit your needs.

Share!

Shares