Code Generator

In this doc it's explained how code generator works, which code is generated and how to customize it.

NOTE: All generated code can be removed with console command explained here: https://github.com/laraloop/docs/blob/master/COMMANDS.md#2-rollback

Configuration

The configuration file can be found in config/infyom/laravel_generator.php. You can define many things such as where generated code will be stored, namespaces and other things.

The most important configuration that you want to change is where code generated will be saved, and which namespace will be used. For this, there are some variables in configuration file that you can change. There are a few combination which you can use, explained below.

1) Save code in app root

The most common way is to save all your code in app root, below you can see how to define variable in this case.

$namespace = 'App';
$basePath = base_path(); 
$appPath = app_path(); 
$prefixPath = 'admin';
$prefixView = 'admin';

The $prefixPath will be used to store in subfolder the code such as controllers, requests, etc. So you can separate different logic of your code, for example admin from frontend. The prefixView it's used as subfolder for views. Is separated because you can set package namespace of views like mypackage::admin. See more below.

2) Save code in package

The second way is to save all generated code inside a package. In this case you can set variable like below:

$namespace = 'VendorName\MyPackage';
$basePath = base_path('packages/vendor-name/my-package');
$appPath = base_path('packages/vendor-name/my-package/src');
$prefixPath = 'admin';
$prefixView = 'mypackage::admin';

When you have separated package for admin and frontend, then you don't need to use prefixes since your package only contain logic of admin. Then you can just set the $prefixView with package namespace. Example:

$namespace = 'VendorName\MyAdminPackage';
$basePath = base_path('packages/vendor-name/my-admin-package');
$appPath = base_path('packages/vendor-name/my-admin-package/src');
$prefixPath = '';
$prefixView = 'my-admin::';

Generated files

In below example was created the resource named "Pages" which you can find in existing Laraloop installation, and it's used for create static content such as privacy, terms and conditions, etc. The resource has this fields: id,slug title, content and timestamps created_at and updated_at.

The below files are modified:

modified:   app/Providers/RouteServiceProvider.php
modified:   resources/views/admin/partials/aside-menu.blade.php
modified:   resources/views/admin/partials/dropdown-menu.blade.php
modified:   routes/web.php

And this are created:

app/Http/Controllers/Admin/PagesController.php
app/Http/Requests/Admin/CreatePagesRequest.php
app/Http/Requests/Admin/UpdatePagesRequest.php
app/Models/Pages.php
app/Policies/PagesPolicy.php
database/migrations/2020_10_08_031323_create_pages_table.php
resources/lang/en/models/pages.php
resources/schemas/Pages.json
resources/views/admin/pages/

Below are the basic content of each files, and once generated, you are free to customize as you wish.

Modified file app/Providers/RouteServiceProvider.php

In route service provider it's added the explicit route model binding and also a route pattern. Read more about route model binding here. This is convenient for several reasons, for example when injecting a model ID to a route or controller action, you will often query to retrieve the model that corresponds to that ID. Using route model binding you can automatically inject the model instances directly into your routes. For example, instead of injecting a user's ID, you can inject the entire User model instance that matches the given ID. And using explicit instead of implicit allow you to customize query as you wish, and creating different variable name. For example for User model Laraloop use several different variable names for each state of User, see here.

In our example for Pages resource it's added below code in RouteServiceProvider.php:

Route::pattern('pages','[0-9]+');

$this->app->router->bind('pages', function ($id) {
    return \App\Models\Pages::findOrFail($id);
});
$this->app->router->bind('onlyTrashedPages', function ($id) {
    return \App\Models\Pages::onlyTrashed()->findOrFail($id);
});
$this->app->router->bind('withTrashedPages', function ($id) {
    return \App\Models\Pages::withTrashed()->findOrFail($id);
});

Modified file aside-menu.blade.php and dropdown-menu.blade.php

In this two blade files it's added the link to Pages resource, one in sidebar menu and other in top dropdown menu.

Modified file routes/web.php

Last modified file is routes. You can find something like below:


// Using Route::resource() for define routes except index and destroy 
Route::group(['prefix' => 'admin'], function () {
    Route::resource('pages', 'Admin\PagesController', ["as" => 'admin'])->except([
      'index', 'destroy'
   ]);
});

// Others custom routes
Route::prefix('admin/pages')
    ->name('admin.pages.')
    ->group(function () {
        Route::get('{status?}', 'Admin\PagesController@index')->name('index')->where(['status' => 'trashed']);
        Route::delete('{pages}/trash', 'Admin\PagesController@trash')->name('trash');
        Route::delete('{onlyTrashedPages}/destroy', 'Admin\PagesController@destroy')->name('destroy');
        Route::patch('{onlyTrashedPages}/restore', 'Admin\PagesController@restore')->name('restore');
        Route::patch('{action}/mass', 'Admin\PagesController@mass')->name('mass');
        Route::get('autocomplete', 'Admin\PagesController@autocomplete')->name('autocomplete');
    });

Created files

For each resource that you create it's created one controller class and 2 requests, for create and update the resource. Then one model class, one policy class for handle authorizations, the migration file and the views. Also you can find the JSON schema of resource that you can use to generate the code again. You are free to customize the JSON file manually as you wish, or to import in Code Generator in admin, for further customization. Then you can regenerate code from command line or via admin area.

Authorizations

For each resource that you create, it's automatically created permissions for all actions, and you can manage which role or user can access and manage the resource.

That is all, I hope this doc has give you the basic understanding of how generator works. Let me know if I missed something or if something is not clear.

Happy code generation!