- How does spatie/laravel-package-tools reduce boilerplate when building Laravel packages?
- The package provides a `PackageServiceProvider` that replaces manual registration of config files, migrations, views, and other assets. Instead of writing repetitive code for `publishes()` or `mergeConfigFrom()`, you define resources declaratively in a single `configurePackage()` method. For example, `hasConfigFile()` and `hasMigrations()` handle all the underlying Laravel plumbing automatically.
- Can I use this package with Laravel 10 or 11? What about older versions?
- The package is designed for modern Laravel versions (10.x and 11.x) and requires PHP 8.1+. Check the [GitHub repository](https://github.com/spatie/laravel-package-tools) for version-specific branches or compatibility notes. Older Laravel versions (e.g., 8.x) may not be supported due to dependency changes, but you can verify by inspecting the `composer.json` constraints.
- How do I add an interactive install command to my Laravel package?
- Use the `hasInstallCommand()` method in your `configurePackage()` block. Pass a closure to configure the `InstallCommand`, where you can chain methods like `publishConfigFile()`, `publishMigrations()`, or `askToStarRepoOnGitHub()`. This generates an Artisan command (e.g., `php artisan your-package:install`) that guides users through setup interactively.
- What’s the difference between `hasAssets()` and manually publishing files in `boot()`?
- The `hasAssets()` method automatically publishes all files in your package’s `resources/assets` directory to the host app’s `public/` folder, using Laravel’s `vendor:publish` system. Manually publishing in `boot()` requires explicit paths and tags, which can be error-prone. `hasAssets()` also integrates with the `InstallCommand` for a seamless user experience.
- Does this package support custom migration paths (e.g., migrations outside `database/`)?
- By default, the package expects migrations in `database/migrations/`, but you can override this with `discoverMigrations()` in your `PackageServiceProvider`. For non-standard paths, use `hasMigration()` with a custom path string. However, deviating from Laravel conventions may require additional configuration or testing to ensure compatibility.
- How do I test a Laravel package built with spatie/laravel-package-tools?
- Test package functionality using Laravel’s testing tools (Pest or PHPUnit). Mock the `PackageServiceProvider` to verify registration logic, and test published assets by simulating `vendor:publish`. For migrations, use `Schema::hasTable()` assertions. The package itself includes unit tests for its core features, which you can reference as examples.
- Will this package work for open-source Laravel packages? Are there versioning concerns?
- Yes, it’s ideal for open-source packages. However, since it ties to Laravel versions, ensure your package’s `composer.json` specifies compatible Laravel ranges (e.g., `^10.0`). Use semantic versioning for your package to match Laravel’s updates. For example, if your package requires Laravel 11, document this clearly to avoid user conflicts.
- Can I customize the directory structure (e.g., move migrations to `app/`)?
- The package enforces a standard structure (`src/`, `resources/`) for consistency, but you can override paths in `configurePackage()`. For example, use `hasMigration('custom/path/to/migration.php')` or `discoverMigrations('app/Migrations')`. However, this may complicate maintenance and could confuse users expecting Laravel’s default layout.
- What if my package needs to register view composers or shared data?
- Use `hasViewComposer()` to register composers (e.g., `hasViewComposer('*', MyComposer::class)`) and `sharesDataWithAllViews()` to pass shared data to views. These methods integrate with Laravel’s view system and are automatically handled during package registration. No manual `View::composer()` or `View::share()` calls are needed.
- Are there alternatives to spatie/laravel-package-tools for Laravel package development?
- Alternatives include rolling your own `ServiceProvider` with manual `publishes()` calls or using `spatie/package-skeleton-laravel` for a pre-configured package boilerplate. However, `laravel-package-tools` stands out for its fluent API, built-in `InstallCommand`, and support for modern Laravel features like view components. It’s the most comprehensive solution for reducing boilerplate in package development.