May 23, 2022 · 3 min read

Adding non-PHP libraries to a Laravel project using Composer

1. Add your library to your composer.json

First, check if your library is available on packagist.org. If it isn’t, we will make it available! In this article, I will use this pure-JS library https://github.com/scratchy/jquery-jsonview as an example, and I’m going to add it to my existing project. Fortunately, this library is already available on packagist.org: https://packagist.org/packages/scratchy/jquery-jsonview. What we only have to do is using composer require scratchy/jquery-jsonview command or manually specifying it in our composer.json to prepare for composer update.

If your library is not available on packagist yet, create a composer.json file inside your library, and publish your library to packagist. Since publishing libraries to packagist won’t be covered in this article, please refer to https://packagist.org/ for procedures to upload a library.

2. Run composer update after specifying new libraries in your composer.json

Your newly added libraries will be available at vendor folder.

3. Create a ServiceProvider

The ServiceProvider class is responsible for copying the libraries to the location where your application can use it. If you are adding a CSS/JS library to your project, the destination is likely to be under public folder.

Let’s use artisan to create the responsible ServiceProvider. For this article’s example, it would be:

php artisan make:provider JsonViewServiceProvider

In the boot() method inside app/Provider/JsonViewServiceProvider.php, add the following code:

$this->publishes([
	__DIR__ . '/../../vendor/scratchy/jquery-jsonview' => public_path() . '/vendor/jquery-jsonview'
], 'debug');

(That /../../ is needed because ServiceProvider class, which will copy folders when artisan vendor:publish command is executed, is lying under app/Provider/, so you have to point it to appropriate direction like you are navigating inside a terminal). The code above will copy vendor/scratchy/jquery-jsonview folder (which was installed by composer update) to public/vendor/ folder when executing php artisan vendor:publish --tag=debug command. If you would like Laravel to copy the library to different location under public folder, simply change /vendor/jquery-jsonview to an according path.

4. Specify your created ServiceProvider in config/app.php

This is necessary for artisan to recognize your newly created ServiceProvider. Inside provider section of config/app.php, specify your ServiceProvider like this:

return [    
	// Other configs above    
	'providers' => [        
		// Other providers        
		'App\Providers\JsonViewServiceProvider',        
		// Other providers    
	],
  // Other configs below
];

5. Run artisan vendor:publish command

At the root of your Laravel project, execute this command:

php artisan vendor:publish --tag=debug

The output should be like this:

Copied Directory [/vendor/scratchy/jquery-jsonview] To [/public/vendor/jquery-jsonview]
Publishing complete for tag [debug]!

6. Verify if your newly added libraries appear under your targeted location!

Congratulation, you have successfully added your non-PHP library to your Laravel project without dealing with npm nodejs gulp webpack stuff!