Install via Composer Template Use the template to scaffold a new package:
composer create-project wayofdev/laravel-package-tpl my-package-name
cd my-package-name
Configure Package Metadata
Edit composer.json to update:
name (e.g., vendor/package-name)descriptionauthorsrequire (Laravel version constraints, e.g., ^10.0)Set Up Environment
Copy .env.example to .env and configure:
cp .env.example .env
APP_NAME, APP_URL, and other environment variables.Run Initial Setup Install dependencies and generate keys:
composer install
php artisan key:generate
First Use Case: Publish a Service Provider
config/app.php (or dynamically via PackageServiceProvider).// config/app.php
'providers' => [
WayOfDev\MyPackage\Providers\PackageServiceProvider::class,
],
Service Provider Integration
Illuminate\Support\ServiceProvider in app/Providers/PackageServiceProvider.php.public function register()
{
$this->app->singleton(MyService::class, fn () => new MyService());
$this->app->bind('my-package', fn () => $this->app->make(MyService::class));
}
Publishing Assets/Config
publishes in PackageServiceProvider:
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/my-package.php' => config_path('my-package.php'),
], 'my-package-config');
}
php artisan vendor:publish --provider="WayOfDev\MyPackage\Providers\PackageServiceProvider" --tag="my-package-config"
Artisan Commands
app/Console/Commands.PackageServiceProvider:
$this->commands([
Commands\MyCommand::class,
]);
php artisan my:command --option=value
Testing with PHPUnit
./vendor/bin/phpunit
tests/TestCase.php:
public function setUp(): void
{
parent::setUp();
$this->app->instance('my-service', Mockery::mock(MyService::class));
}
Docker Integration
docker-compose.yml for local development.docker-compose up -d
docker-compose exec app bash
Leverage Laravel’s Package Discovery
composer.json includes:
"extra": {
"laravel": {
"providers": ["WayOfDev\\MyPackage\\Providers\\PackageServiceProvider"],
"aliases": {
"MyPackage": "WayOfDev\\MyPackage\\Facades\\MyPackage"
}
}
}
config/app.php.Dynamic Configuration
config() helper with a fallback:
$value = config('my-package.key', 'default');
Event Listeners
PackageServiceProvider:
Event::listen(MyEvent::class, MyListener::class);
event(new MyEvent($data));
Blade Directives
PackageServiceProvider:
Blade::directive('myDirective', function ($expression) {
return "<?php echo customLogic($expression); ?>";
});
API Resources
Illuminate\Http\Resources\Json\JsonResource in app/Http/Resources.Namespace Conflicts
WayOfDev\MyPackage\...).Autoloading Issues
composer dump-autoload after adding new classes.autoload in composer.json:
"autoload": {
"psr-4": {
"WayOfDev\\MyPackage\\": "app/"
}
}
Service Provider Boot Order
priority in register() for critical dependencies:
$this->app->register(AnotherServiceProvider::class, ['priority' => 10]);
Testing Pitfalls
App facade or services in tests to avoid side effects:
$this->app->instance('app', Mockery::mock('overload:App'));
RefreshDatabase trait for database tests:
use Illuminate\Foundation\Testing\RefreshDatabase;
Docker Volumes
docker-compose.yml mounts the correct volumes for live-reload:
volumes:
- ./:/var/www/html
Log Output
\Log::debug('Debug message', ['context' => 'key']);
storage/logs/laravel.log.Xdebug in Docker
docker-compose.yml:
services:
app:
extra_hosts:
- "host.docker.internal:host-gateway"
php.ini for Xdebug:
xdebug.mode=debug
xdebug.start_with_request=yes
Artisan Command Debugging
--verbose flag:
php artisan my:command --verbose
Custom GitHub Actions
.github/workflows/ with additional workflows (e.g., for deployment).PHPStan Rules
phpstan.neon:includes:
- vendor/wayofdev/laravel-package-tpl/phpstan/
Docker Compose Overrides
docker-compose.override.yml for environment-specific configs.Laravel Mix/Webpack
webpack.mix.js for frontend assets (if applicable).Package Bootstrap
bootstrap/app.php for global package initialization:
$app->register(\WayOfDev\MyPackage\Providers\PackageServiceProvider::class);
How can I help you explore Laravel packages today?