DEV Community

Cover image for Laravel Package : Easy creation with Laravel package
Raymond Baghumian
Raymond Baghumian

Posted on

Laravel Package : Easy creation with Laravel package

A Laravel package generator that streamlines the process of creating standardized package structures. Quickly bootstrap your packages with a complete, well-organized structure and focus on building functionality rather than configuration.

Features

  • Generates a complete Laravel package structure with a single command
  • Creates standard Laravel package directories (config, migrations, views, etc.)
  • Sets up composer.json with proper autoloading and dependencies
  • Creates a Service Provider with common Laravel integrations pre-configured
  • Includes testing setup with PHPUnit
  • Generates license, readme, and other essential files

Installation

You can install the package via composer:

composer require rayiumir/laravel-package 
Enter fullscreen mode Exit fullscreen mode

After Publish Config Files:

php artisan vendor:publish --provider="Rayiumir\LaravelPackage\ServiceProvider\PackageServiceProvider" 
Enter fullscreen mode Exit fullscreen mode

The service provider will be automatically registered for Laravel 5.5+. For older versions, add the service provider manually:

// config/app.php

'providers' => [ Rayiumir\LaravelPackage\ServiceProvider\PackageServiceProvider::class, ]; 
Enter fullscreen mode Exit fullscreen mode

Usage

Basic Usage

Generate a new package with the following command:

php artisan make:package my-package 
Enter fullscreen mode Exit fullscreen mode

This will create a new package in the packages/my-package directory with the default vendor name.
Customizing the Vendor Name

You can specify a custom vendor name:

php artisan make:package my-package --vendor=acme 
Enter fullscreen mode Exit fullscreen mode

Including Tests

To include PHPUnit test setup:

php artisan make:package my-package --with-tests 
Enter fullscreen mode Exit fullscreen mode

Generated Structure

The generated package will have the following structure:

packages/my-package/ ├── config/ ├── database/ │ └── migrations/ ├── resources/ │ ├── lang/ │ └── views/ ├── routes/ ├── ServiceProvider/ │ └── PackageNameServiceProvider.php ├── tests/ (if --with-tests option is used) │ ├── Feature/ │ ├── Unit/ │ └── TestCase.php ├── composer.json ├── LICENSE.md ├── README.md └── phpunit.xml 
Enter fullscreen mode Exit fullscreen mode

Next Steps After Generation

After generating your package, you might want to:

  • Edit the composer.json file to update package details and requirements
  • Modify the Service Provider to add any specific functionality
  • Update the README.md with your package documentation
  • Add your migrations, routes, and views
  • Create your package's main classes in the src directory
  • If you used the --with-tests option, start writing tests for your package

Local Development

When developing the package locally within a Laravel application, you can add the repository to your application's composer.json:

"repositories": [ { "type": "path", "url": "./packages/my-package" } ] 
Enter fullscreen mode Exit fullscreen mode

Testing Your Package

If you generated your package with the --with-tests option, you can run tests with:

cd packages/my-package composer install vendor/bin/phpunit 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)