ViewMaker Artisan Command Package for Laravel 5.2 Released

Access all tutorials in sprocket icon.

Published April 10, 2016 by Bill Keck.

ViewMaker for Laravel 5.2 Released

Many times in development, you will follow a specific pattern, such as creating a route resource and a matching controller that has the following methods:

  • index
  • create
  • store
  • show
  • edit
  • update
  • destroy

The great news is that in laravel we have artisan commands that will quickly generate the controller file, class and stubs for us, giving us just the right amount of code to get started. It’s a real time-saver.

As great as that is, I found myself wishing for something to quickly stub out views. What I had been doing is the past is copying and pasting a typical pattern where I had the view folder, and 4 to 6 blade files, depending on whether or not I was using the datatables jquery plugin to make a searchable, sortable datagrid.

The problem with that approach is that it’s really easy to make a mistake and include the wrong variable or route, and you end up spending a lot of time debugging, so you lose a lot of efficiency.

Another approach to that is to make code snippets and templates in your IDE, which is a very good way to go for many tasks. But with the above pattern, you need to make at least 4 files plus a folder, so using templates will still take time.

Another way to go is to set up gists of your code so you can always refer to them. This works, but you still have to manually create a lot files.

So that was the problem I was trying to solve when I came up with ViewMaker, which is an artisan plugin that you download via composer. I had a lot of fun making this.

To pull it into your project, run the following from your command line:



composer require evercode1/view-maker

Then in config/app.php, add the following to your providers array:



Evercode1\ViewMaker\ViewMakerServiceProvider::class,

I wrote a full tutorial on the github page, so you can check that out as well. The command is structured as follows:



php artisan make:views {modelName} {masterPageName} {templateType}

So if you had a model named Widget and a master page named master.blade.php, you could run:



php artisan make:views widget master plain

And that would create the view folder and files for you.

I decided to cover 3 scenarios for creating views that match a REST pattern, plain, basic and dt. Plain, basic and dt represent the different types of templates the command will produce.

The plain templates are essentially just a stub, so if you don’t want Bootstrap or don’t want to build upon the minimal markup that is included with basic and dt, then use ‘plain’ in your command and you get the view folder and the following stubs:

  • index
  • create
  • show
  • edit

If you want the basic templates, run:



php artisan make:views widget master basic

The basic templates give you the same number of files and a little more, for example, you get a create form and edit form and a formatted show page. The views use a very light implementation of Bootstrap, so you will have to pull in Bootstrap as a dependency in your master page.

If you want the jquery datatables implementation, run:



php artisan make:views widget master dt

You will get the following files:

  • index
  • create
  • show
  • edit
  • datatable
  • datatable-script

This will give you a working datagrid on your index page that is searchable and sortable. This requires the datatables jquery plugin, so make sure you have that dependency pulled in through your master page or related files. There are a couple of other requirements as well, such as the api route and controller and the csrf token in the meta tag.

I have sample code for this implementation and the tutorial here on Github. I recommend you read the full tutorial if you are not already familiar with datatables. You can see the datatables part of the tutorial here.

Also check out the tutorial for conventions, such as multi-word models. For example, if you have a SomethingElse model, you would express that as something-else in the command.

While the make:views command will make the view folder and files for you, it will not create the model, migration, route, and controller that you need to actually view the views. Those you have to do on your own, but luckily you have artisan commands for most of that too.

Well, I had a lot of fun making this tool. I hope you find it useful. Click on the sprocket icon at the top of the page to see all my Laravel articles and tutorials. Please comment, share, like and review if you can, thanks.

I don’t have a donate button, but If you would like to support my work and learn more about Laravel, you can do so by buying one of my books, Laraboot: laravel 5.2 For Beginners, I really appreciate it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s