Generating Components

Using Blueprint's artisan commands you may generate multiple Laravel components from a draft file. The draft file contains a definition using a YAML syntax, with a few shorthands for convenience.

By default, the blueprint:build command attempts to load a draft.yaml (or draft.yml) file. While you are welcome to create your own draft files, it's common to simply reuse the draft.yaml file over and over again to continue to generate code for your application.

Draft file syntax

Within the draft file you define models and controllers using an expressive, human-readable YAML syntax.

Let's review the following draft file:

models:
  Post:
    title: string:400
    content: longtext
    published_at: nullable timestamp

controllers:
  Post:
    index:
      query: all
      render: post.index with:posts

    store:
      validate: title, content
      save: post
      send: ReviewNotification to:post.author with:post
      dispatch: SyncMedia with:post
      fire: NewPost with:post
      flash: post.title
      redirect: post.index

This draft file defines a model named Post and a controller with two actions: index and store.

While this YAML might seem dense at first, the syntax aligns nicely with the same syntax you'd use within Laravel. For example, all of the column data types are the same you would use when creating columns in a migration.

In addition, the statements within each controller actions use familiar terms like validate, save, and fire.

Blueprint also leverages conventions and uses shorthands whenever possible. For example, you don't need to define the id, created_at, and updated_at columns. Blueprint automatically generates these for models.

You also don't have to specify the Controller suffix when defining a controller. Blueprint automatically appends it when not present. All of this aligns with Blueprints goal of rapid development.

Generated code

From just these 20 lines of YAML, Blueprint will generate all of the following Laravel components:

  • A model class for Post complete with fillable, casts, and dates properties, as well as relationships methods.
  • A migration to create the posts table.
  • A factory intelligently set with fake data.
  • A controller class for PostController with index and store actions complete with code generated for each statement.
  • Resource routes for the PostController actions.
  • A form request of StorePostRequest validating title and content based on the Post model definition.
  • A mailable class for ReviewNotification complete with a post property set through the constructor.
  • A job class for SyncMedia complete with a post property set through the constructor.
  • An event class for NewPost complete with a post property set through the constructor.
  • A Blade template of post/index.blade.php rendered by PostController@index.
  • An HTTP Test complete with respective arrange, act, and assert sections for the PostController actions.

While this draft file only defines a single model and controller, you may define multiple models and controllers.