laravel-dev-env-setup
Laravel Development Environment Setup
Overview
This is a hybrid environment with the web browser and VS Code IDE installed in Win11 and all other dev tools installed in WSL2.
Win11: VS Code IDE with recommended extensions
WSL2: PHP, MariaDB/Postgres, Laravel, VS Code remote development extensions
This environment does not use docker inside WSL as VS Code extensions cannot work inside docker. Hence all the dev tools are installed natively in WSL and VS Code will automatically detect/install required extensions to work with WSL.
Software Used
- Windows 11
- Ubuntu 22.04 in WSL
- VS Code
- Laravel 10.x
Installation Steps
- Install Ubuntu 22.04 LTS in WSL
- Install required packages
apt install php-cli php-common php-curl php-mbstring php-mysql php-xml php-zip - Development packages
apt install git - Database
apt intall mariadb-serverorapt install postgresql - Download the latest
composerpackage to/usr/local/bin - Use composer to install laravel.
Installing Language Support for VS Code
- Install Laravel IDE Helper for full VS Code intellisense support
composer require --dev barryvdh/laravel-ide-helper- Add
/_ide_helper*to.gitignoreto ensure ide-helper files are not committed into the Git repo - Run
php artisan clear-compiled - Run
php artisan ide-helper:generateto create Facades - Run
php artisan ide-helper:modelsto create Models - Use the default "no" when prompted.
- In
composer.jsonadd the below to ensure IDE helper PHP files are recreated whenever composer updates dependencies.
"scripts": {
"post-update-cmd": [
"@ide-helper"
],
"ide-helper": [
"@php artisan ide-helper:generate",
"@php artisan ide-helper:models -N"
]
}
- You can now
composer run ide-helperanytime to recreate the ide-helper files.
PHP Code Formatting Using Pint
- Laravel Pint is automatically installed in Laravel v10
- Install the Run on Save VS Code extension
- In VS Code
settings.json, add the following code to run pint whenever any.phpfile is saved.
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.php$",
"cmd": "${workspaceFolder}/vendor/bin/pint ${file}"
}
]
}
Rejecting non-Pint Compliant Code in Git
A git pre-commit hook can be used to reject any PHP code is not compliant with Pint.
Note that Pint does not support blade templates at this time.
Setting up the git hooks directory in the project root directory is useful as it makes it available for every member of the project team. This provides a consistent environment and behavior for everyone which is great.
- Setup a new
.githooksfolder and copy thepre-commitbash script for customization.
mkdir .githooks
cp .git/hooks/pre-commit.sample .githooks/pre-commit
- Add the below lines to the top of the
.githooks/pre-commitbash script.
# Ensure the committed files are Pint compliant.
echo "Running Laravel Pint..."
if ! ./vendor/bin/pint --dirty --test
then
echo "Pint errors detected, git commit aborted."
exit 1
fi
- Change the git hooks directory from the default
.git/hooksto.githooksby runninggit config core.hooksPath .githooks. To automate this for other developers, add the below JSON snippet to thecomposer.json/scripts/post-autoload-dumpsection. Thepost-autoload-dumpscripts are executed whenever composerinstallorudpatecommands is invoked. See Composer Scripts.
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi",
"git config core.hooksPath .githooks"
],