Creating a Laravel Preset Package, Part Two

Published September 23rd, 2018
5 minute read
This was written over two years ago, so some information might be outdated. Frameworks and best practices change. The web moves fast! You may need to adjust a few things if you follow this article word for word.

We'll add some custom functionality to the composer package we created in part one to create a Laravel boilerplate with things set up just how we prefer.

Part Two, Doing the Work

We will cover how to configure the preset to remove compiled files from version control
and have it automatically set up a better default layout for Laravel

Our first order of business will be to remove compiled assets from version control :skull:. Storing minified files in our git repository should be avoided. Since we can automatically generate those files with our build process, they aren't true "source" files.

A convention many of the community presets adopt is storing the new versions of files within our package's src/stubs directory, so we'll follow that with our example too.

Create a file at src/stubs/new-gitignore containing the following:


We just added the public/css, public/js, and public/mix-manifest.json paths on top of Laravel's default.

Now tell our Preset class to replace the default .gitignore file.
Open up the src/Preset.php file and update the install() method with the following.

public static function install()
    // Replace the default .gitignore with our own
    copy(__DIR__ . '/stubs/new-gitignore', base_path('.gitignore'));

If we were to run php artisan preset austencam we should see the changes get made to our main app's .gitignore file, pretty slick!

Let's do something more interesting now, like replacing the default views with a simple "app" layout.

Add a new file at this location in our package: src/resources/views/layouts/app.blade.php:

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="csrf-token" content="{{ csrf_token() }}" />
            Note: I'm pulling Tailwind CSS from the CDN to shorten this tutorial  
            but most apps will want to build it with their own config, see
   for more details.
    <title>{{ config('', 'Laravel') }}</title>
  <body class="font-sans text-green antialiased text-grey-dark">
    <div id="app">

This is a simple layout our app's views can inherit from. The default welcome.blade.php the framework comes with is meant to be ripped out in favor of your favorite frontend framework anyway, so let's make our new welcome view use the new layout.

Create a file at src/stubs/resources/views/welcome.blade.php with this code:

@extends('') @section('body')
<div class="min-h-screen flex items-center justify-center">
  <h1 class="text-3xl">Build Something Great!</h1>

The only thing left to do is tell our preset to copy these files over. Open up the src/Preset.php file and add the following use statement.

use Illuminate\Filesystem\Filesystem;

Now update the install() method in our src/Preset.php file to match this code:

    public static function install()
        // Replace the default .gitignore with our own
        copy(__DIR__ . '/stubs/new-gitignore', base_path('.gitignore'));

        // Delete the default view and copy our stub views into place
        tap(new Filesystem, function ($files) {
            $files->copyDirectory(__DIR__.'/stubs/resources/views', resource_path('views'));

So let's break down the code we added there (starting with tap). Some of you may be thinking "whoa, what the heck is tap?". Tap is a helper function that allows us to easily string together groups of actions without needing to use a temporary variable. It's a matter of personal preference really... I think of it as "we're taking a filesystem object, and performing these actions with it", so using tap makes a lot of sense in this context.

Here are a couple other ways we could've written these lines:

// One way
$files = new Filesystem;

// Another way.. but not the same object :(
(new Filesystem)->delete(...);
(new Filesystem)->copyDirectory(...);

Anyway, run our php artisan preset austencam command again. We should see our new views in the resources/views folder of the main app. Feel free to open up your app in the browser and check it out too!

Now we've got an idea of how to build any Laravel preset / boilerplate we want. Taking this knowledge further, your preset might contain custom vue components, the CSS framework of your choice, or any number of other customizations!

Let's Review

We've covered how to create a composer package in part one, how to extend the php artisan preset command with our own preset, and how to configure that preset to set up a new app just how we'd like it. Now we can easily require our package from any Laravel app and to set up the app like we want in seconds. This is a great way to create your own boilerplate so you can more rapidly iterate on your ideas. Looking forward to seeing what you folks come up with!

Although my real preset does a few more things, I kept it simple for the sake of focusing on the process in this article. Laravel's base Preset class also includes a few useful methods you'll want to check out if you're building your own preset.

Please, if you run into problems or have any questions, feel free to reach out to me on twitter. I'd love to help. Thanks!

Enjoy this article? Follow me on Twitter for more tips, articles and links.
😢 Awww, nobody has liked or mentioned this on Twitter yet.

Join the Newsletter ❤️

A most excellent monthly newsletter with code & design tips, curated links and more!
Don't worry, I'll never send you spam. Unsubscribe at any time.