Skip to content

Instantly share code, notes, and snippets.

@abelcallejo
Last active July 5, 2024 15:09
Show Gist options
  • Save abelcallejo/139445a5b77d4105375de093a88129c9 to your computer and use it in GitHub Desktop.
Save abelcallejo/139445a5b77d4105375de093a88129c9 to your computer and use it in GitHub Desktop.
Creating a starter kit for Laravel

Laravel logo Composer logo

Creating a starter kit for Laravel

You are probably wondering how Laravel made some starter kits to be easily installable using just the following commands:

composer install laravel/breeze     # Quickly integrates the starter kit to the composer environment
php artisan breeze:install          # Quickly integrates the starter kit to the Laravel environment

If you want to build a project with the same installation setup, you are on the right track. This documentation is all about doing exactly just that. Without further ado, let's just jump right into it!

Creating the starter kit

  1. Prepare the working directory

    mkdir /path/to/packagenamehere
    cd /path/to/packagenamehere
  2. Make the working directory as a composer package

    composer init

    Simply provide the values for some prompt:

    • Package name
      • Syntax: <vendor>/<name>
      • Example: vendornamehere/packagenamehere
      • See reference
    • Description
      • Example: This is the most minimal Laravel starter kit. Built for the sake of demo.
      • See reference
    • Author
      • Syntax: <Display Name>/< <email_address> >
      • Example: Juan dela Cruz <juan@gmail.com>
      • See reference
    • Etc
  3. Require the following packages:

    • illuminate/console
    • illuminate/support
  4. Write your install command handler

    /path/to/packagenamehere/src/Console/InstallCommand.php

     <?php
    
     namespace VendorNameHere\PackageNameHere\Console;
    
     use Illuminate\Console\Command;
    
     class InstallCommand extends Command
     {
         /**
          * The name and signature of the console command.
          *
          * @var string
          */
         protected $signature = 'packagenamehere:install';
    
         /**
          * The console command description.
          *
          * @var string
          */
         protected $description = 'Description of your command';
    
         /**
          * Execute the console command.
          *
          * @return int
          */
         public function handle()
         {
             echo "Hello world!";
             return Command::SUCCESS;
         }
     }
  5. Write your service provider

    /path/to/packagenamehere/src/Support/PackageNameHereServiceProvider.php

    <?php
    
    namespace VendorNameHere\PackageNameHere\Support;
    
    use Illuminate\Contracts\Support\DeferrableProvider;
    use Illuminate\Support\ServiceProvider;
    
    class PackageNameHereServiceProvider extends ServiceProvider implements DeferrableProvider
    {
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register() {
            //
        }
    
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot() {
            if (! $this->app->runningInConsole()) {
                return;
            }
    
            $this->commands([
                \VendorNameHere\PackageNameHere\Console\InstallCommand::class,
            ]);
        }
    
        /**
         * Get the services provided by the provider.
         *
         * @return array
         */
        public function provides() {
            return [\VendorNameHere\PackageNameHere\Console\InstallCommand::class];
        }
    
    }
  6. Make your service provider discoverable by specifying the extra option.

    /path/to/packagenamehere/composer.json

    {
    ...
        "extra": {
          "laravel": {
             "providers": [
                 "VendorNameHere\\PackageNameHere\\Support\\PackageNameHereServiceProvider"
             ]
          }
        },
     ...
     }

    See reference

Magic It's working

Installing a local composer package

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment