This article presents an alternative approach to developing Laravel packages: building them outside the Laravel framework itself. This method promotes better isolation, simplifying testing and debugging.
A Two-Factor Authentication Package Example
The article uses a two-factor authentication package as a practical demonstration. The complete package is available here.
Development Workflow:
repositories
key in composer.json
allows referencing this custom package before it's on Packagist:{ "repositories": [ { "type": "vcs", "url": "https://github.com/Whyounes/laravel-two-factor-auth-demo" } ] }
Then, require the package in your main application's composer.json
:
{ "require": { "Whyounes/laravel-two-factor-auth-demo": "dev-master" } }
composer.json
): Create the package's composer.json
file, defining its metadata, dependencies (including illuminate
components and a service like Twilio), and autoloading:{ "name": "whyounes/laravel-two-factor-auth", "autoload": { "psr-4": { "Whyounes\TFAuth\": "src" } } // ... other details }
Directory Structure: Organize your package's files logically (example structure provided in the article).
Laravel Provider: Use a Laravel service provider (src/Providers/TwoFAProvider.php
) to handle package registration, including loading migrations, merging configurations, and registering bindings.
Testing: Thorough testing is crucial. The article recommends orchestra/testbench
for testing Laravel aspects of your package.
Version Tagging: Tag your package releases using Git tags (e.g., v1.0.0
) to track versions.
Continuous Integration (CI): Integrate a CI tool (like TravisCI) to automate testing across different PHP versions. A .travis.yml
file configures the CI process.
Benefits of this Workflow:
This approach offers improved package isolation, easier debugging, and wider applicability beyond just Laravel projects. The article stresses the importance of testing and using CI for robust package development. The author encourages community contribution by sharing well-tested, reusable packages. The FAQs section further clarifies common questions regarding Laravel package development.
The above is the detailed content of An Alternative Laravel Package Development Workflow. For more information, please follow other related articles on the PHP Chinese website!