MauroBaptista.com

Notes to Set Up a New Laravel Project

November 14, 2021

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:

  alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'
  alias shell="sail shell"
  alias shellr="sail root-shell"
  alias tinker="sail tinker"
  alias dusk="sail dusk"
  alias sn="sail node"
  alias snpm="sail npm"
  alias sc="sail composer"
  alias sp="sail php"
  alias a="sail artisan"
  alias p="sail test"
  alias 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):

{
    ...
    
    "scripts": {
        ...
        
        "package:outdated": "@composer outdated -D -f json",
        "test:run": "./vendor/bin/phpunit",
        "test:coverage": "@test:run --coverage-html tests/report",
        "phpcs:check": "php ./bin/phpcs.phar ./phpcs.xml app/",
        "phpcs:fix": "php ./bin/phpcbf.phar ./phpcs.xml app/",
        "phpmd:check": "php ./bin/phpmd.phar app/ ansi ./phpmd.xml",
        "phpcpd:check": "php ./bin/phpcpd.phar app/",
        "psalm:check": "php ./vendor/bin/psalm",
    },
    
    ...
}

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:

APP_NAME={proper value}
APP_URL={proper value}

CACHE_DRIVER=file
QUEUE_CONNECTION=redis
SESSION_DRIVER=cookie

MAIL_FROM_ADDRESS={proper value}

FILESYSTEM_DRIVER=s3
AWS_ACCESS_KEY_ID=sail
AWS_SECRET_ACCESS_KEY=password
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=local
AWS_ENDPOINT=http://minio:9000
AWS_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)


Mauro Baptista

Mauro Baptista

Building stuff with code and having fun in the way

© 2021, Built with Gatsby