How to create a Backpack add-on with a custom Operation

Say you’ve created a custom operation, and want to share it with other developers around the world. How do you do that? Well, it’s pret...

Karan Datwani
Karan Datwani

Say you’ve created a custom operation, and want to share it with other developers around the world. How do you do that? Well, it’s pretty easy, actually. We have a full tutorial that you can follow, but for this particular use case – only an operation, it’s even easier.

Assuming you’ve already got your operation ready (e.g. the Email operation we’ve taught in a previous article), you only need to do the following steps:

Step 1. Use our package skeleton.

We'll use Laravel Packager to generate the boilerplate for our new package. Let's install it using the following command:

composer require jeroen-g/laravel-packager --dev

Now let's create our package. Instead of using their skeleton, we're going to use the Backpack addon-skeleton:

php artisan packager:new --i --skeleton=""

It will then ask you for some basic information about the package.

  • vendor-name: your GitHub handle (or organisation), in kebab-case (ex: company-name);
  • package-name: package-name should be in kebab-case too (ex: custom-operation);
  • skeleton: the above command already has it, the URL; just hit ENTER;
  • website: website URL including the protocol,;
  • description: a short description of your package;
  • license: MIT, GPLv2; our skeleton assumes you want MIT, but you can easily change it;

The above command will:

  • create a /packages/vendor-name/package-name folder in your root directory;
  • specify our new package path in our project's composer.json, under repositories, to actually load the files;

You'll notice the new folder looks identical to a Laravel project, with a few understandable exceptions.

Note: You can now test that your package is being loaded using dd('got here') inside your package's AddonServiceProvider::boot() method.

Step 2. Define your package dependencies (if any).

If your package needs any third-party packages apart from Backpack and Laravel, make sure to add them to the require section. Normally this just means cutting & pasting the line from your project's composer.json to your package's composer.json.

In our case, we don't have any.

Step 3. Move the operation-related files from your project to your package.

Move operation files from the project directory to the same directory inside your package's src directory; in our example, we move:

  • email.blade.php to packages/{vendor-name}/{package-name}/resources/views/buttons;
  • email_form.blade.php to packages/{vendor-name}/{package-name}/resources/views/operations;
  • EmailOperation.php to packages/{vendor-name}/{package-name}/src/Http/Controllers/Operations;

Now, open all the files you've moved and manually update:

  • namespace like App\Http with your {VendorName}\{PackageName}\Http;
  • view path like crud:: with {vendor-name}.{package-name}::

Test that the package works & delete the empty package folders. That's it, your package is ready to be published.

Step 4. Packagist - put the package online & make it public.

Note: Use the same name you defined in your package's composer.json.

Step 5. Reinstall the package like a regular user.

Run the following command inside the project's root directory to remove the locally created package.

php artisan packager:remove vendor-name package-name

And now install it exactly the same as your users will. You can use Composer's --prefer-source flag so that it pulls the actual GitHub repo:

composer require vendor-name/package-name --prefer-source

Congratulations! You now have your brand-new Composer package, up and running. Now let everybody in the community know about it, by telling us in our Gitter, on Reddit's r/Laravel, and on our GitHub.

Want to receive more articles like this?

Subscribe to our "Article Digest". We'll send you a list of the new articles, every week, month or quarter - your choice.

Reactions & Comments

What do you think about this?

Latest Articles

Wondering what our community has been up to?