Composer Packages that Every Laravel Developer Must Have

Written by dkhorev | Published 2022/09/13
Tech Story Tags: web-development | laravel | composer | php | php-development | productivity | programming | framework

TLDRTelescope Horizon Laravel IDE Helper Larastan PHP_Codeshiftervia the TL;DR App

Its rich ecosystem makes Laravel Framework a great choice for PHP development. There are 15+ standalone extensions to the framework that solve common problems of big and complex apps.

Some of them are very specific, like Laravel Cashier, which provides an easy Stripe integration for your projects.

Some of them are useful to projects of any size and complexity, and some of them allow us to build performant and scaling apps. Those extensions I will describe in this part of the series.

I will start with absolute must-haves.

I believe developers of middle and senior levels should always use those in their process where you have at least one external data store and/or expose any sort of APIs.


Telescope

https://laravel.com/docs/9.x/telescope#introduction

Everything that happens in your applications will be recorded and displayed here. This is a useful tool for finding bottlenecks in your apps. If your API response is slow — is it a database problem or a compute problem? What logs did that job call created? What tasks have the scheduler run in the last 5 minutes? All sorts of information can be found here.

One hint: add the Telescope trim job right away, to keep debug tables smaller.

$schedule->command('telescope:prune --hours=48')->daily();

Horizon

https://laravel.com/docs/9.x/horizon#introduction

This requires Redis and your project to be a bit bigger than a starter template. Horizon adds asynchronous task processing to your Laravel application. This allows your API/web pages to respond faster while offloading heavy work to Horizon instances.

Must have for any async queues processing, for WebSockets integration in your apps.

Has a nice feature of metrics to spot slow-running async jobs.


Now, we’ll go over developer productivity addons for your Laravel projects.

Laravel IDE Helper

https://github.com/barryvdh/laravel-ide-helper

This will create a helper file to resolve all of your model fields so PhpStorm is aware of them. This will create a helper description file for all of the magic Eloquent methods (resolving methods and auto-completion, parameters hints).

Recommended commands:

php artisan ide-helper:model --nowrite

php artisan ide-helper:meta

php artisan ide-helper:generate

Add to your .gitingore file

.phpstorm.meta.php

_ide_helper.php

_ide_helper_models.php

And some automation — add to your composer.json scripts

Every time you run composer i or composer update — your models are refreshed.

Larastan

https://github.com/nunomaduro/larastan

A static code analysis tool designed for Laravel projects (under the hood PhpStan is used, so effectively you get both).

Add to your composer.json scripts

"analyze": ["vendor/bin/phpstan analyse"]

Now running the composer analyze command will test your code for any problems.

This command can be run during CI/CD cycle to ensure code quality on a project with a distributed team.

Pro Tip: you can get PhpStorm to automatically analyze working files and show any errors. We’ll see how this can be configured in the next article.

PHP_CodeSniffer

https://github.com/squizlabs/PHP_CodeSniffer

These tools will help keep your code to a selected standard. When used in CI/CD will enforce code standards on a team of developers for a project.

Add to your composer.json scripts

"lint": ["vendor/bin/phpcs --parallel=8"],

"lint-fix": ["vendor/bin/phpcbf --parallel=8"],

Now running the composer lint command will test your code for any code standard violations.

Running composer lint-fix will fix any errors that are possible to fix automatically.

Pro Tip: you can get PhpStorm to automatically analyze working files and show any code standard violations. We’ll see how this can be configured in the next article.

Slevomat Coding Standard for PHP_CodeSniffer

https://github.com/slevomat/coding-standard

This package extends PHP_CodeSniffer with a few custom rules, that help keep code organized when the team uses different IDEs with different settings.

You will either have phpcs.xml in your project or can create one. Just add anything you prefer to that file.

Examples:

This will clean up any unused imports from your files.

<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
  <properties>
    <property name="searchAnnotations" value="true"/>
  </properties>
</rule>

Report useless variables in your code:

<rule ref="SlevomatCodingStandard.Variables.UselessVariable"></rule>

How many spaces do you want to see around anonymous function declarations?

<rule ref="SlevomatCodingStandard.Functions.ArrowFunctionDeclaration">
  <properties>
    <property name="spacesCountAfterKeyword" value="0"/>
    <property name="spacesCountBeforeArrow" value="1"/>
    <property name="spacesCountAfterArrow" value="1"/>
    <property name="allowMultiLine" value="true"/>
  </properties>
</rule>

And so on. The list of available inspections is really large.

composer lint and composer lint-fix commands will automatically apply newly added rules.


Notable mentions will help you solve common problems when growing your project.

  • Nova — a CRUD admin for your app, extensible with custom components.
  • Passport / Sanctum — easy auth flows for your apps using battle-tested standards. Sanctum is for simple apps. Passport — for complex apps.
  • Socialite — easy social login integration via OAuth.
  • Scout — full-text search solution for Laravel, provides a driver-based solution so you can do it with your local DB or external specialized services.


A bonus — for deploying your apps. I see people trying very hard to deploy their simplest apps and MVPs on complex cloud infrastructures. But for starters, this simple service can take you very far…

Recommended for first-time deployments and MVPs, developers, or teams without dedicated DevOps roles. A much cheaper solution than AWS + Terraform, or any other kind of CI/CD automation.

Forge

https://forge.laravel.com/

Internally it works on pre-configured EC2 servers (or any servers you can provide), but it takes out of the way a lot of obstacles for people new to DevOps.

Such as:

  • provides Nginx proxy to serve static content, serve php-fpm threads
  • easy php version switch
  • free HTTPS certificate config and renews
  • runs Laravel Scheduler for you
  • one-click and zero-downtime deploys
  • runs Horizon for async work (and Redis)

Hope this was helpful. Good luck and happy engineering!


Also published here.


Written by dkhorev | Sharing my ideas on software development (NestJS, Laravel, System Design, DevOps for backend engineers)
Published by HackerNoon on 2022/09/13