Introducing the blueprint?
Blueprint is an open-source tool for rapidly generating multiple Laravel components from a single, human-readable definition.
Blueprint has two driving principles:
- Increase Development Speed
- Promote Laravel Conventions
Blueprint Features
- Draft your application with a simple syntax.
- Generate code using artisan commands.
- Output multiple Laravel components at once.
Blueprint requires a Laravel application running version 6.0 or higher.
How to install and use BluePrint?
- Using the below command install the Laravel-shift blueprint package.
- composer require -W –dev laravel-shift/blueprint
- Using the below command create a draft.yaml
- php artisan blueprint:new
- After creating a draft.yaml, you can define multiple types of actions. you may define multiple models and controllers in your own draft files.
- 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.
- draft.yaml file example
models:
Post:
title: string:400
content: longtext
published_at: nullable timestamp
controllers:
Post:
index:
query: all
render: post.index with:posts
create:
render: post.create
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
- The above 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.
- In addition, the statements within each controller action use familiar terms like validate, save, and fire.
- You don’t need to define the id, created_at, and updated_at columns in your models. Blueprint automatically generates these.
- You can also define the foreign key column in a draft.yaml file, using the below code to create an index on the column. In addition, it will generate code to add the reference and cascade “on delete” constraints. For example :
- user_id: id foreign:users
Model relationships are defined in the draft.yaml file:
- Blueprint also allows you to define many of the relationships available within Laravel, including: belongsTo, hasOne, hasMany, and belongsToMany.
- To define one of these relationships, you may add a relationships section to your model definition. Within this section, you specify the relationship type followed by a comma-separated list of model names.
models:
Post:
title: string:400
published_at: timestamp nullable
relationships:
hasMany: Comment
belongsToMany: Media, Site
belongsTo: \Spatie\LaravelPermission\Models\Role
Generating Database Seeders using the draft.yaml file:
- Blueprint also supports defining a seeders section within a draft file to generate database seeders for a given model.
- The syntax for this section is simply seeders: value, where value is a comma-separated list of model references.
models:
Post:
title: string:400
content: longtext
published_at: nullable timestamp
Comment:
post_id: id
content: longtext
user_id: id
User:
name: string
seeders: Post, Comment
Some useful controller statements for the draft.yaml file:
- Blueprint comes with an expressive set of statements that define code within each controller action, but also additional components to generate.
- Dispatch
- Generates a statement to dispatch a Job using the value to instantiate an object and pass any data. For example:
- dispatch: SyncMedia with:post
- Fire
- Generates a statement to dispatch an Event using the value to instantiate the object and pass any data. For example:
- fire: NewPost with:post
- Generates a statement to dispatch an Event using the value to instantiate the object and pass any data. For example:
- Notify
- Generates a statement to send a Notification using the value to instantiate the object, specify the recipient, and pass any data. For example:
- notify: post.author ReviewPost with:post
- Generates a statement to send a Notification using the value to instantiate the object, specify the recipient, and pass any data. For example:
- Redirect
- Generates a return redirect() statement using the value as a reference to a named route passing any data parameters. For example:
- redirect: post.show with:post
- Generates a return redirect() statement using the value as a reference to a named route passing any data parameters. For example:
- Render
- Generates a return view(); statement for the referenced template with any additional view data as a comma-separated list. For example:
- render: post.show with:post,foo,bar
- Generates a return view(); statement for the referenced template with any additional view data as a comma-separated list. For example:
- Resource
- Generates response statement for the Resource to the referenced model. You may prefix the plural model reference with collection or paginate to return a resource collection or paginated collection, respectively. For example:
- resource: user
- resource: paginate:users
- Generates response statement for the Resource to the referenced model. You may prefix the plural model reference with collection or paginate to return a resource collection or paginated collection, respectively. For example:
- Respond
- Generates a response that returns the given value. If the value is an integer, Blueprint will generate the proper response() statement using the value as the status code. Otherwise, the value will be used as the name of the variable to return. For example:
- respond: post.show with:post
- Generates a response that returns the given value. If the value is an integer, Blueprint will generate the proper response() statement using the value as the status code. Otherwise, the value will be used as the name of the variable to return. For example:
- Send
- Generates a statement to send a Mailable or Notification using the value to instantiate the object, specify the recipient, and pass any data. For example:
- send: ReviewPost to:post.author with:post
- Generates a statement to send a Mailable or Notification using the value to instantiate the object, specify the recipient, and pass any data. For example:
- Validate
- Generates a form request with rules based on the referenced model definition. You may use a value of the model reference to validate all columns or a comma-separated list of the column names to validate. For example:
- validate: post
- validate: title, content, author_id
- Generates a form request with rules based on the referenced model definition. You may use a value of the model reference to validate all columns or a comma-separated list of the column names to validate. For example:
- Dispatch