Generating Components
Blueprint provides artisan
commands to 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 multiple draft files, it's common to simply reuse the draft.yaml
file over and over 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
. You may, of course, define multiple models and controllers in your own draft files.
At first, this YAML may seem dense. But its 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 in your models. Blueprint automatically generates these.
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 Blueprint's 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 withfillable
,casts
, anddates
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
withindex
andstore
actions complete with code generated for each statement. - Resource routes for the
PostController
actions. - A form request of
StorePostRequest
validatingtitle
andcontent
based on thePost
model definition. - A mailable class for
ReviewNotification
complete with apost
property set through the constructor. - A job class for
SyncMedia
complete with apost
property set through the constructor. - An event class for
NewPost
complete with apost
property set through the constructor. - A Blade template of
post/index.blade.php
rendered byPostController@index
. - An HTTP Test complete with respective arrange, act, and assert sections for the
PostController
actions.