Laravel Proto Generator

Laravel Proto Generator is a Laravel package used for building scaffolds. Primary goal is to be used for fast prototyping for CRUD applications. It can generate simple outputs as a starting point. Also, with complex templates it is possible to make complete solutions.

In this tutorial we will go through process of building scaffolding template.

To install package open composer.json file and add following line:

"require-dev": {
    "dam1r89/proto-generator": "2.0.*"
},

open app/config.php to add providers:

dam1r89\ProtoGenerator\ProtoGeneratorServiceProvider::class,
Illuminate\Html\HtmlServiceProvider::class

and aliases

'Form' => Illuminate\Html\FormFacade::class,
'HTML' => Illuminate\Html\HtmlFacade::class,

Create scaffold folder in the root of the project. Here we will create a blueprint for our views, models, controller and any other resource required for our scaffold. This folder will be in some sense merged with main application, and it should have same folder structure.

First we will create template for eloquent model. Create this folder structure: app/Models and create file __model__.php

__model__ will be replaced with appropriate model name which is created from command parameter. This will be explained later.

Open __model__.php file and paste template.

<?php namespace __$namespace__Models;

use Illuminate\Database\Eloquent\Model;

class __$model__ extends Model
{
    public $fillable = [__!foreach($fields as $field):__ '__$field__',__!endforeach;__];

}

All variables in templates are predefined. Based on parameter that you specify in the command Proto Generator creates different variation of the same keyword. These are the variations you can use when creating template: table, item, itemLower, singleItem, model, controller, collection, migrationDate, fields.

For example, with keyword video_tag or video_tags these variations are available:

table => video_tags
singleItem => video_tag
collection => videoTags
controller => VideoTags
item => videoTag
itemLower => videotag
model => VideoTag

So for example, if you need a variable for table name you would use __ $table __ and if you need controller name you would write __ $controller __Controller.

Everything that is in double underscores is normal php code which will be execute when command for generating is run. Normal php tags will not be executed and that will become a part of the output file. __ <php_code> __ is similar to <?php echo <php_code> ?>. Use this for printing data, and for other non printing php functions use __! <php_code __ syntax.

To run and see how it works run command:

php artisan proto user --fields='{"name":{}, "body":{}}' -t scaffold

If look at your app/Models folder you will see Article.php file with generated model class.

Now we will create database migration for Article model. In Laravel migrations are found in database/migrations folder, and usually they have a datetime prefix and appropriate create_table label. With proto generator you can use variable migrationDate to get required prefix. So we can create file with this format __migrationDate___create___table___table.php. Edit file and put this content.

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Create__$controller__Table extends Migration {

    public function up()
    {
        Schema::create('__$table__', function(Blueprint $table){
            $table->increments('id');
            __!foreach($fields as $field):__$table->string('__$field__');
            __!endforeach;__ $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('__$table__');
    }

}

This will create roughly formated migration, so you will want to change field types, default values and any additional tuning, however it is good starting point.

To scaffold application, type again:

php artisan proto user --fields='{"name":{}, "body":{}}' -t scaffold

and now, migration is ready. After running php artisan migrate we have our table created.

Making templates for controller and views is in the next post.

Author

I plan to write more articles about common laravel components. If you are interested let’s stay in touch.
comments powered by Disqus