Appearance
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
Run the following command to create a brand-new Laravel application:
shell
composer create-project laravel/laravel appname
Set up Git
Go to the directory and initialize git:
shell
cd appname
git init --initial-branch=main
git add .
git commit -m "Initial commit."
Install code quality tools
Configure Laravel Pint by creating a pint.json
file in the root directory:
json
{
"preset": "psr12",
"rules": {
"new_with_parentheses": {
"anonymous_class": false,
"named_class": true
},
"braces_position": {
"anonymous_classes_opening_brace": "next_line_unless_newline_at_signature_end"
},
"concat_space": {
"spacing": "one"
}
},
"exclude": [
"bootstrap",
"config",
"public"
]
}
Run Pint to check if it changes the User.php
model correctly by spliting the traits into separate lines:
shell
vendor/bin/pint
Commit the changes:
shell
git add .
git commit -m "Set up Pint."
Install Larastan:
shell
composer require larastan/larastan --dev
Create a new file phpstan.neon
in the root directory with the following content:
neon
includes:
- ./vendor/larastan/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
Commit your changes so far:
shell
git add .
git commit "Install Larastan and PHPMD."
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
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'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, assign them the CheckAdmin
middleware:
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
DB_CONNECTION=mariadb
DB_HOST=mariadb
DB_DATABASE=app_name
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 "Set up admin users."
Install additional composer packages
Install some nice composer packages as necessary. Make sure to also 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 # may be outdated
composer require venturecraft/revisionable
composer require lab404/laravel-impersonate
Frontend
Install Laravel Breeze
Run the following commands:
shell
composer require laravel/breeze
php artisan breeze:install blade
git add .
git commit "Install Laravel Breeze."
Convert to Bootstrap
In package.json
, delete the following packages:
text
@tailwindcss/forms
alpinejs
autoprefixer
postcss
tailwindcss
Install Bootstrap:
shell
npm i bootstrap sass -D
Delete the files postcss.config.js
and tailwind.config.js
.
In vite.config.js
, replace resources/css/app.css
with resources/scss/app.scss
.
Delete the following directories:
text
app/View
resources/css
resources/views
Copy the files from breezebootstrap/resources
into your application's resources
directory, which contains three subdirectories:
text
js
scss
views
In /routes/web.php
, update the home
route:
php
Route::view('/', 'home')->name('home');
Run Vite and test your changes:
shell
npm run dev
Commit your changes:
shell
git add .
git commit "Convert to Bootstrap."
Install Essence
shell
git submodule add https://.../essence.git resources/essence
git add .
git commit "Install Essence."
Set up SASS files
In resources/scss/app.scss
, edit as follows to standardize theming:
scss
// Variables
@import 'variables';
// Import Essence
@import '../essence/scss/essence';
// Vendors
@import 'vendors';
// General styles
@import 'styles';
Commit your changes:
shell
git add .
git commit "Set up asset building."
Install Font Awesome (or an icon package of your preference)
shell
npm install @fortawesome/fontawesome-free -D
In resources/scss/_vendors.scss
, 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';
Add misc code for supercharged development
In AppServiceProvider.php
, add the following code in the boot()
method:
php
public function boot(): void
{
Model::shouldBeStrict(! $this->app->isProduction());
Model::unguard();
Paginator::useBootstrap();
}