Setting up Laravel like a ninja
Full-stack developer
After thousands of commits, hundreds of thousands of lines of code, and countless hours of sweat coding and debugging, I’ve learned a thing or two about Laravel and personally, I think this is the best way to set up a new Laravel project.
TL;DR: This is how you set up Laravel like a ninja.
Getting started
First things first, you need to install Laravel.
My go-to is usually:
composer create-project laravel/laravel my-project
I like to break things down into steps, so I can easily see what’s going on. Rather than using the Laravel installer, I prefer using Composer to create a new Laravel project.
Next, the ones that are always needed:
cp .env.example .env
php artisan key:generate
Now… let’s get to the good stuff.
Laravel Sail
While I think composer dev
is a great way to get started with Laravel, I personally would always go for Sail and think it is the best way to get started with Laravel.
Laravel Sail, for those who don’t know, is a utility tool and has inbuilt Docker images for Laravel. Docker is one of those things that just works, and you’ll never escape it—it’s used everywhere. If you’ve never used Docker before, you should.
composer require laravel/sail --dev
We can then choose the services we want to install with Docker:
php artisan sail:install
I typically choose:
[x] Mysql
[x] Redis
[x] Mailpit
I then usually add an alias to my .bashrc
or .zshrc
file:
alias sail='sh $([ -f sail ] && echo sail || echo vendor/bin/sail)'
With that, you can now launch the Laravel Sail environment with docker-compose via:
sail up -d
It is optional, but I’d recommend publishing the Docker files so you can see what’s going on:
php artisan sail:publish
PHP-CS-Fixer (Laravel Pint)
I like to install a Code style enforcer / fixer known as PHP CS Fixer.
Laravel recently released a new package called Laravel Pint which is a wrapper around PHP CS Fixer.
composer require laravel/pint --dev
You can run this command and it’ll automatically fix your code style:
./vendor/bin/pint
Github Action
If you are using Github, this is an action I’d usually add to enforce the code style at .github/workflows/php-code-style.yml
:
name: PHP Code Style
on:
pull_request:
branches: [ "main" ]
merge_group:
types: [ checks_requested ]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: "laravel-pint"
uses: aglipanci/[email protected]
with:
preset: laravel
verboseMode: true
testMode: true
pintVersion: 1.18.1
Correspond this with your pint version in the composer.json
.
PHPStan (Larastan)
I also like to install PHPStan, which is a static analysis tool for PHP.
There is a community supported package called Larastan which is a wrapper around PHPStan for Laravel.
composer require --dev "larastan/larastan:^2.0"
You need to create a config file for PHPStan at phpstan.neon
:
includes:
- vendor/larastan/larastan/extension.neon
parameters:
paths:
- app/
# Level 9 is the highest level
level: 5
# ignoreErrors:
# - '#PHPDoc tag @var#'
#
# excludePaths:
# - ./*/*/FileToBeExcluded.php
#
# checkMissingIterableValueType: false
It’s up to you what level. I usually go with 5/6. I have projects in 9 but it’s a bit too strict for me.
You can then run this command to check your code:
./vendor/bin/phpstan analyse --memory-limit=2G
Github Action
Likewise with this, you can add a Github action to enforce PHPStan at .github/workflows/phpstan.yml
:
name: PHP Code Quality
on:
pull_request:
branches: [ "main" ]
merge_group:
types: [ checks_requested ]
jobs:
phpstan:
name: phpstan
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
coverage: none
- name: Install composer dependencies
uses: ramsey/composer-install@v3
- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
Coverage Reports
It’s optional but if you are running tests, you might want to generate coverage reports and a nice way to showcase these are via badges on your README.
I like to use Codecov for this. You can sign up for a free account and get a token.
Here’s what my open-source project Minecraftmagic looks like:
Honourable mentions
Things that are worth mentioning but I don’t always use them:
- Laravel Pail (Dive into your Laravel application’s log files directly from the console)
- PhpStorm (IDE)