Creating a Laravel 5 Package

Published on Author craigzearfossLeave a comment

For this example we will use the vendor craigzearfoss and the package bullets.

  1. In your project folder create the package folder packages/VENDOR_NAME/PACKAGE_NAME.
  2. Inside of the package folder create a directory named src.
  3. Use artisan to create a service provider.
    php artisan make:provider BulletsServiceProvider
    
  4. The service provider will be in the app/Providers directory. Move it to the packages/VENDOR_NAME/PACKAGE_NAME/src directory and change the namespace at the top of the provider file to your vendor package. The file should look something like this.
    <?php
    
    namespace Craigzearfoss\Bullets;
    
    use Illuminate\Support\ServiceProvider;
    
    class ReviewServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap the application services.
         *
         * @return void
         */
        public function boot()
        {
            //
        }
    
        /**
         * Register the application services.
         *
         * @return void
         */
        public function register()
        {
            //
        }
    }
    
  5. If you have database migrations:

    1. Create the migrations as usual with the artisan make:migration command.
    2. Move your migrations to the directory packages/VENDOR_NAME/PACKAGE_NAME/resources/migrations
    3. In the boot method of your service provider add the following:
      public function boot()
      {
          $this->publishes([
              realpath(__DIR__.'/../resources/migrations') => database_path('migrations'),
          ], 'migrations');
      }
      
    4. To use the migration you will have to publish as described in step 11.
  6. Create your package files.

    • Routes: put your routes in an Http/routes.php file.
    • Controllers: Add your controllers in the directory Http/Contollers
    • Config file. You can create a config file in the src directory (e.g. bullets.php). Note: To use the config file you will have to publish as described in step 11.
    • Views: Create a new folder call view in the package folder, ie. in the packages/VENDOR_NAME/PACKAGE_NAME folder
  7. Create the composer.json file for the package.

    composer init

    For our example it will look something like this:

    {
        "name": "craigzearfoss/bullets",
        "description": "Add bullet points to a model",
        "authors": [
            {
                "name": "Craig Zearfoss",
                "email": "craigzearfoss@yahoo.com"
            }
        ],
        "minimum-stability": "dev",
        "require": {}
    }
    
  8. Add the package path in root composer.json. For example:

        "psr-4": {
            "App\\": "app/",
            "Jai\\Contact\\": "packages/craigzearfoss/bullets/src/"
        }
    
  9. Add the service provider in your project’s app/config.php file in the providers section. For example:
    Craigzearfoss\Bullets\BulletsServiceProvider::class,
    
  10. Load the package.

    composer dump-autoload
    
  11. If you created a migrations or a config file for the package then publish it by running the following artisan command.

    php artisan vendor:publish
    

    This will push your config file (bullets.php => project/config/bullets.php) and then you can access it.

  12. You can now access your package via the url – “YOUR_WEBSITE/PACKAGE_NAME

Leave a Reply

Your email address will not be published. Required fields are marked *