Skip to content
On this page

Boilerplate

Good job finding this secret page! This magical documentation spells out how I personally approach creating a new Laravel application. Feel free to use it for your reference.

Essential

Install Laravel and set up version control

Run the following command to create a brand-new Laravel application.

shell
composer create-project laravel/laravel appname

Go to the directory and initialize git.

shell
cd appname
git init
git add .
git commit -m "Initial commit."

If you use Gitflow, create the develop branch.

shell
git checkout -b develop

Install necessary composer packages

Install some nice composer packages that you may need. Make sure to follow their docs to set them up properly.

shell
composer require laravel/telescope
composer require barryvdh/laravel-debugbar --dev
composer require barryvdh/laravel-ide-helper --dev
composer require venturecraft/revisionable
composer require lab404/laravel-impersonate

Install code quality tools

Install PHPCS (known formally as PHP_CodeSniffer).

shell
composer require squizlabs/php_codesniffer --dev

Create a new file phpcs.xml in the root directory with the following content.

xml
<?xml version="1.0"?>
<ruleset name="PSR12">
    <description>The PSR12 coding standard</description>

    <rule ref="PSR12"/>

    <exclude-pattern>vendor</exclude-pattern>
    <exclude-pattern>node_modules/</exclude-pattern>
    <exclude-pattern>database/</exclude-pattern>
    <exclude-pattern>resources</exclude-pattern>
    <exclude-pattern>storage/</exclude-pattern>
    <exclude-pattern>app/Console/Kernel.php</exclude-pattern>

    <arg name="colors"/>
    <arg name="parallel" value="75"/>
    <arg value="np"/>
</ruleset>

When you want to use PHPCS to check code quality, run the following command.

shell
composer exec phpcs app

Install Larastan.

shell
composer require nunomaduro/larastan --dev

Create a new file phpstan.neon in the root directory with the following content.

neon
includes:
    - ./vendor/nunomaduro/larastan/extension.neon

parameters:

    paths:
        - app

    # The level 9 is the highest level
    level: 5

    checkMissingIterableValueType: false

When you want to use Larastan to check code quality, run the following command.

shell
composer exec phpstan analyze app

Install PHPMD.

shell
composer require phpmd/phpmd --dev

Create a new file phpmd.xml in the root directory with the following content.

xml
<?xml version="1.0"?>
<ruleset name="My first PHPMD rule set"
         xmlns="http://pmd.sf.net/ruleset/1.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
         xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
    <description>Leo's standard phpmd.xml for Laravel</description>

    <rule ref="rulesets/cleancode.xml">
        <exclude name="StaticAccess"/>
        <exclude name="ElseExpression"/>
    </rule>

    <rule ref="rulesets/codesize.xml"/>
    <rule ref="rulesets/controversial.xml"/>

    <rule ref="rulesets/design.xml">
        <exclude name="CouplingBetweenObjects"/>
    </rule>
    <rule ref="rulesets/design.xml/CouplingBetweenObjects">
        <properties>
            <property name="maximum" value="20"/>
        </properties>
    </rule>

    <rule ref="rulesets/naming.xml">
        <exclude name="ShortVariable"/>
        <exclude name="LongVariable"/>
    </rule>
    <rule ref="rulesets/naming.xml/ShortVariable">
        <properties>
            <property name="minimum" value="3"/>
            <property name="exceptions" value="i,j,e,f,id,db"/>
        </properties>
    </rule>

    <rule ref="rulesets/unusedcode.xml">
        <exclude name="UnusedFormalParameter"/>
    </rule>
</ruleset>

When you want to use PHPMD to check code quality, run the following command.

shell
composer exec phpmd app text phpmd.xml

Set up admin users

If your application uses admin users, edit the migration create_users_table.php and add a boolean is_admin field to designate admin users.

php
$table->boolean('is_admin')->default(false);

In the User model, add the boolean cast for the is_admin attribute.

php
protected $casts = [
    'is_admin' => 'boolean',
];

Add the CheckAdmin middleware.

shell
php artisan make:middleware CheckAdmin
php
public function handle($request, Closure $next)
{
    // Is there's no authenticated admin user, redirect to home
    if (! $request->user() || ! $request->user()->is_admin) {
        return redirect()->route('home');
    }

    return $next($request);
}

To restrict certain routes for admins, set the CheckAdmin middleware to them.

php
Route::prefix('/admin')->middleware(CheckAdmin::class)->group(function () {
    Route::get('/', [AdminController::class, 'dashboard')->name('admin.dashboard');
});

Set up database and environment

Create your development database and edit the .env file.

dotenv
APP_NAME="App Name"
APP_URL=http://url.local
LOG_CHANNEL=single # for simple logging
DB_CONNECTION=mysql
DB_HOST=mysql
DB_DATABASE=app
DB_USERNAME=root
DB_PASSWORD=root

Run migration and laravel-ide-helper (if you've installed it) to test database connection.

shell
php artisan migrate
php artisan ide-helper:models -WR

Commit your changes so far.

shell
git add .
git commit "Install composer packages and set up admin users."

Frontend

Scaffold UI and authentication:

Run the following commands.

shell
composer require laravel/ui
php artisan ui bootstrap --auth
npm install
npm run dev

Ignore the built assets and the NPM lock file in .gitignore:

/public/css
/public/js
/public/mix-manifest.json
package-lock.json

Commit your changes.

shell
git add .
git commit "Scaffold UI and authentication."

Install Bootstrap and nice frontend packages

shell
npm install bootstrap -D
npm install @fortawesome/fontawesome-free -D

To use FontAwesome, create the /resources/sass/vendors.scss file and add the following.

scss
@import '@fortawesome/fontawesome-free/scss/fontawesome';
@import '@fortawesome/fontawesome-free/scss/regular';
@import '@fortawesome/fontawesome-free/scss/solid';
@import '@fortawesome/fontawesome-free/scss/brands';

Set up Vite

Edit the vite.config.js like below:

js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import laravel from 'laravel-vite-plugin'
const path = require('path')

export default defineConfig({
  plugins: [
    vue(),
    laravel({
      input: [
        'resources/sass/app.scss',
        'resources/sass/public.scss',
        'resources/js/app.js'
      ],
      refresh: true,
    }),
  ],
  resolve: {
    alias: {
      '~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap'),
    }
  },
})

In resources/sass/app.scss, edit as follows to standardize theming:

scss
// Fonts
@import url("https://fonts.googleapis.com/css?family=Roboto:400,500,700");

// Variables
@import 'variables';

// Vendors
@import 'vendors';

// Essence and Bootstrap
@import '../essence/scss/variables';
@import '~bootstrap/scss/bootstrap';
@import '../essence/scss/essence';

If you use Essence, add /resources/essence to your .gitignore file.

Run Vite to test your configuration.

shell
npm run dev

Commit your changes.

shell
git add .
git commit "Install Bootstrap, FontAwesome, and set up asset building."

Update templates to use Blade Components

Move views/layouts to views/components/layouts.

In app.blade.php, update the title tag and replace @yield('content') with slot.

php
<title>{{ $title ?? 'Home' }} | {{ config('app.name') }}</title>
php
<main class="py-4">
    {{ $slot }}
</main>

Update the view files under views/auth to use the new app component.

php
<x-layouts.app title="Login">
    <div class="container">
        ...
    </div>
</x-layouts.app>>

Commit your changes.

shell
git add .
git commit "Update views."

Use strict mode

In AppServiceProvider.php, add:

php
Model::shouldBeStrict(! $this->app->isProduction());
Model::unguard();