MauroBaptista.com

Notes to Set Up a New Laravel Project

Laravel Tools

Set up a new project

curl -s "https://laravel.build/my-cool-new-project?with=mysql,redis,minio,mailhog" | bash

Know more about the services at https://laravel.com/docs/8.x/installation#choosing-your-sail-services


Code Analysis

Go inside the folder, and create a new bin folder, then bring some useful tools: mkdir bin and cd bin

PHP Mess Detector:

wget -c https://phpmd.org/static/latest/phpmd.phar

Configuration file: phpmd.xml

PHP Code Sniffer:

wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar

wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

Configuration file: phpcs.xml

PHP Copy & Paste Detector:

wget https://phar.phpunit.de/phpcpd.phar

If not, it would be good to create some aliases to help:

1alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'
2alias shell="sail shell"
3alias shellr="sail root-shell"
4alias tinker="sail tinker"
5alias dusk="sail dusk"
6alias sn="sail node"
7alias snpm="sail npm"
8alias sc="sail composer"
9alias sp="sail php"
10alias a="sail artisan"
11alias p="sail test"
12alias pf="sp ./vendor/bin/phpunit --filter "

Start sail: sail up (or sail up -d)

Require the Laravel Debugbar:

sc require barryvdh/laravel-debugbar --dev

Require and set up the Psalm:

sc require --dev vimeo/psalm sp ./vendor/bin/psalm --init

sc require --dev psalm/plugin-laravel sp ./vendor/bin/psalm-plugin enable psalm/plugin-laravel

Adding scripts in composer

Add the following scripts in the composer.json (this will help the CI/CD):

1{
2 ...
3
4 "scripts": {
5 ...
6
7 "package:outdated": "@composer outdated -D -f json",
8 "test:run": "./vendor/bin/phpunit",
9 "test:coverage": "@test:run --coverage-html tests/report",
10 "phpcs:check": "php ./bin/phpcs.phar ./phpcs.xml app/",
11 "phpcs:fix": "php ./bin/phpcbf.phar ./phpcs.xml app/",
12 "phpmd:check": "php ./bin/phpmd.phar app/ ansi ./phpmd.xml",
13 "phpcpd:check": "php ./bin/phpcpd.phar app/",
14 "psalm:check": "php ./vendor/bin/psalm",
15 },
16
17 ...
18}

Publish Stubs/Resources

I like to clean up my stubs, removing some comments an adding some missing types:

a stub:publish

If running Livewire: livewire:stubs

I also publish some items that I like to have control over:

a publish:vendor

Then publish: laravel-mail laravel-notification laravel-pagination

If running Livewire: livewire


Environment Variables

The variables I change in the .env file:

1APP_NAME={proper value}
2APP_URL={proper value}
3
4CACHE_DRIVER=file
5QUEUE_CONNECTION=redis
6SESSION_DRIVER=cookie
7
8MAIL_FROM_ADDRESS={proper value}
9
10FILESYSTEM_DRIVER=s3
11AWS_ACCESS_KEY_ID=sail
12AWS_SECRET_ACCESS_KEY=password
13AWS_DEFAULT_REGION=us-east-1
14AWS_BUCKET=local
15AWS_ENDPOINT=http://minio:9000
16AWS_USE_PATH_STYLE_ENDPOINT=true

Config Minio

As the .env above, go to the Minio, and create a new public bucket called local


Tests

Remove the DB_ comments on phpunit.xml, as for most of the cases using sqlite is good enough.

To run tests:

p It will run all test suite

pf something It will run tests with something (--filter)

sc test:coverage It will run tests with coverage. The files will be in the /tests/report

(Do not forget to add /tests/report in the .gitignore file)