Laravel Packages sit at the heart of every serious PHP project in 2026, and the recent hijacking of the laravel-lang namespace proved that convenience without verification is a liability. This guide walks you through building, publishing, and defending packages in a year where supply-chain attacks have become the single biggest threat to the PHP ecosystem.
Overview & Core Question
What is a Laravel Package?
A Laravel package is a reusable Composer-distributed library that plugs into the framework's service container. It can ship routes, migrations, config files, views, console commands, facades, or service providers. Packages range from a single trait to full SaaS-style SDKs, and they live on Packagist, GitHub, or private Git remotes that Composer can reach.
Definition: A Laravel Package is a versioned PHP library loaded through Composer that extends a Laravel application by registering services, routes, config, or views via a service provider.
Why Build Your Own Package?
Reuse beats copy-paste. Once a piece of logic (say, an audit logger or a tax calculator) appears in three projects, the cheapest insurance is a private package with its own test suite and version history. You also gain a single source of truth for bug fixes, which is impossible when the same snippet is copy-pasted across repos.
When to Use Existing Packages
Reach for community packages when the problem is generic and battle-tested: authentication (Sanctum, Passport), admin panels (Filament, Backpack), or media handling (Spatie MediaLibrary). The decision rule: if the package is older than 12 months, has more than 1,000 stars, and is maintained by a known organization, the build-vs-buy math almost always favors install.
Why Laravel Packages Matter in 2026
Three forces are reshaping how teams treat Laravel Packages this year: an exploding supply-chain attack surface, regulatory pressure on dependency provenance, and an AI-assisted productivity boom that has flooded Packagist with new libraries.
Ecosystem Growth & Supply-Chain Trends
Packagist now hosts over half a million packages, and roughly 8,000 new ones were added in the first half of 2026. The growth is good news for productivity and bad news for attackers, who only need one compromised maintainer account to reach thousands of downstream projects. The active exploitation of the laravel-lang packages in May 2026 demonstrated exactly that risk, with a tag-rewriting attack that pushed credential-stealing malware into installs after 2026-05-22 22:32 UTC.
Regulatory & Compliance Drivers
The EU's NIS2 directive and the US White House's 2025 secure-software memo now require organizations to maintain a Software Bill of Materials (SBOM) for production systems. Composer can generate one through composer sbom plugins, and auditors routinely ask for it. Treating packages as auditable assets, not opaque dependencies, is now a board-level concern.
2026 Developer Productivity Gains
New tooling in the Laravel installer v5.31.0 line automatically matches the local PHP version in GitHub Actions workflows and supports a --no-node flag for backend-only setups. Combined with Laravel Boost skill packs, the time from idea to publishable package has dropped from a day to under an hour for experienced maintainers.
Prerequisites & Security Foundations
Required Tools & Versions
You need PHP 8.3 or higher, Composer 2.7+, Git 2.40+, and Laravel Installer 5.31.0 or newer. Node.js is optional; the new installer propagates the LARAVEL_INSTALLER_NO_NODE=1 environment variable so you can scaffold projects on servers that intentionally exclude npm.
Composer & Packagist Setup
Authenticate Composer against Packagist with an API token stored in ~/.composer/auth.json. Never commit that file. Add a config.platform block to lock the PHP version so CI and local installs cannot silently drift.
Version Control & CI Basics
Use a signed git workflow: enable GPG or SSH commit signing and protect the main branch with required reviews. Pair that with a GitHub Actions pipeline that runs composer audit on every pull request.
Step-by-Step Package Creation Workflow
Initialize Skeleton with Laravel Installer
Run laravel package my-new-package from the parent directory. The command clones the official laravel/package-skeleton, scrubs the git history, installs dependencies, and executes the configuration script. Flags like --config, --routes, and --views pre-scaffold the directories you actually need.
Define Service Providers & Facades
The skeleton ships a PackageServiceProvider in src/. Register bindings, merge config, publish assets, and conditionally load routes using the loadRoutesFrom, loadMigrationsFrom, and loadViewsFrom helpers. Facades are optional; prefer dependency injection unless a static call is significantly cleaner.
Configure Autoload & Composer Scripts
Open composer.json and set PSR-4 mapping for "MyVendor\\MyNewPackage\\": "src/". Avoid the autoload.files array whenever possible: every file listed there is auto-included on every request, which is exactly how the laravel-lang dropper in src/helpers.php got executed silently.
Write Tests & CI Pipeline
Use Orchestra Testbench for unit and feature tests, and ship a Pest suite for readability. Configure GitHub Actions with the installer-generated workflow, then add a composer audit step plus a Snyk scan. Cache ~/.composer/cache to keep builds under three minutes.
Packaging Best Practices & Feature Comparison
Namespace & PSR-4 Compliance
Namespaces must mirror the directory tree 1:1. A class at src/Http/Middleware/TrimStrings.php belongs in MyVendor\MyNewPackage\Http\Middleware\TrimStrings. Skip PSR-4 and Composer will autoload failures; skip PSR-12 and contributors will not touch your code.
Semantic Versioning & Dependency Pinning
Tag releases with SemVer: 1.4.2. Breaking changes bump the major. Pin Laravel itself with "laravel/framework": "^11.0|^12.0" rather than *, and use composer require --no-update when adjusting constraints so the lock file stays deterministic.
Comparing Laravel-Package-Skeleton vs Custom Boilerplate
| Feature | Laravel-Package-Skeleton | Custom Boilerplate |
|---|---|---|
| Initial setup time | ~30 seconds via CLI | ~2 hours of manual config |
| Testbench integration | Pre-wired | Manual wiring required |
| GitHub Actions workflow | Auto-generated, PHP version matched | Hand-rolled |
| Upgrade path for Laravel releases | Tracks upstream | Maintenance burden on you |
| Risk of stale patterns | Low (community-maintained) | High if you stop updating |
The skeleton wins for 90% of new packages. Custom boilerplate only makes sense when you have a non-standard layout (such as a Symfony bundle bridge) that the skeleton cannot express.
Real-World Trade-offs & Performance Insights
Bundle Size vs. Feature Richness
A heavy package that pulls in Guzzle, Symfony Process, and a queue worker can add 6 MB to your autoloader. Composer mitigates this with --prefer-dist and Composer's flat dependency tree, but the rule still holds: every require is a future CVE. Profile with composer why-not and trim anything you can rebuild in 20 lines.
Lazy Loading & Service Container Optimization
Bind expensive services as singletons in the container rather than resolving them in the service provider's boot() method. Use $this->app->bindIf() so application-level bindings can override package defaults, and defer non-critical work behind an event listener that fires only when the relevant feature is requested.
Deployment on Cloud Platforms
AWS, GCP, Azure, DigitalOcean, Heroku, Vercel, Netlify, Railway, and Fly.io all support PHP 8.3 builds with Composer cache layers. Vercel and Netlify are edge-first and poorly suited to long-running queue workers, so choose Fly.io or AWS ECS for packages that ship a Horizon dashboard or a custom daemon. HashiCorp Vault plus Kubernetes secrets beats .env files for any production deployment.
Security Risks: Tag Rewriting & Composer Autoload
The May 2026 laravel-lang incident is the single most important case study for any package author this year, so the rest of this section walks through it in detail.
Tag Rewriting Attack Vectors
Attackers compromised the maintainer account, then rewrote every git tag in laravel-lang/lang, laravel-lang/http-statuses, laravel-lang/attributes, and laravel-lang/actions to point at malicious commits in attacker-controlled forks. Because Composer's composer.lock references tags, not commit hashes by default, any composer update after 2026-05-22 22:32 UTC pulled the tampered code. Packagist eventually removed the bad versions, but the window of exposure lasted long enough to hit hundreds of pipelines.
Composer Autoload Execution Risks
The dropper was embedded in src/helpers.php, an entry in the autoload.files directive. Composer's autoloader includes that file on every request, so the malicious code ran before any application logic. It fingerprinted the host, wrote a marker file, disabled TLS verification, decoded the C2 domain flipboxstudio.info at runtime, and fetched a secondary payload from flipboxstudio[.]info/payload. On Windows it launched a .vbs file with cscript; on Linux and macOS it used PHP's exec() for a background process. The secondary payload was a ~5,900-line PHP credential stealer spanning 15 modules, encrypting harvested data with AES-256 before exfiltrating to flipboxstudio[.]info/exfil.
Monitoring & Auditing with Snyk & GitHub Actions
Add a Snyk step to your CI using the official GitHub Action. Snyk cross-references your composer.lock against its advisory database and flags known compromised versions. Pair it with composer audit, GitHub's Dependabot security updates, and a custom action that diffs composer.lock against a known-good snapshot. Tools like Aikido Security, Socket, and StepSecurity provide deeper runtime monitoring, including secret-leak detection in CI logs.
Common Pitfalls & Troubleshooting Guide
Composer Lock File Issues
If composer install fails with "hash mismatch", your lock file is out of sync with composer.json. Run composer update --lock in a clean environment, review the diff, and commit the new lock. Never edit composer.lock by hand; it is a deterministic artifact, not a config file.
Namespace Conflicts
Two packages using the same App\Services namespace will collide at autoload time. Namespace your code under a vendor-specific root, and add conflict entries in composer.json when you know a package cannot coexist with yours. The composer prohibits command lists incompatible packages currently installed.
CI/CD Build Failures
Most CI failures come from missing PHP extensions (intl, pcntl, sodium) or wrong platform versions. Pin the PHP version in CI to match production, and cache vendor/ keyed by composer.lock hash. If GitHub Actions times out on Windows runners, start the queue listener with php artisan queue:listen --timeout=0, a Windows-specific fix added in the installer to keep the listener alive.
Who Should Use This Guide? Personas & Recommendations
| Target Persona | Recommended Option | Key Reason & Real-World Benefit |
|---|---|---|
| Solo developer shipping a side project | Laravel-Package-Skeleton with default CI | Zero boilerplate, push to Packagist in under an hour, automatic PHP version matching in GitHub Actions |
| Mid-size team with shared internal libraries | Private Packagist mirror plus Snyk + composer audit | Catches compromised public packages before they reach production, lets you enforce code review on internal packages |
| Enterprise integration with compliance needs | Custom SBOM pipeline on AWS or GCP with HashiCorp Vault, signed git tags, and a dedicated security reviewer | Meets NIS2 and SBOM requirements, isolates secrets, enables signed-tag enforcement to defeat tag-rewriting attacks |