Article available in English
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
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.
Your newly added libraries will be available at
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
artisan to create the responsible
ServiceProvider. For this article’s example, it would be:
php artisan make:provider JsonViewServiceProvider
boot() method inside
app/Provider/JsonViewServiceProvider.php, add the following code:
$this->publishes([__DIR__ . '/../../vendor/scratchy/jquery-jsonview' => public_path() . '/vendor/jquery-jsonview'], 'debug');
/../../ 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.
This is necessary for
artisan to recognize your newly created
provider section of
config/app.php, specify your
ServiceProvider like this:
// Other configs above
'providers' => [
// Other providers
// Other providers
// Other configs below
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]!
Congratulation, you have successfully added your non-PHP library to your Laravel project without dealing with npm nodejs gulp webpack stuff!