If you want to use a package in Laravel, you simply add a single line in composer.json file and the job is done. This is because the package is available in packagist.org, what if the package that you want to use is a non-Composer Laravel package? Maybe available on a git repo or a private repo or so. In this post, we will see how to install and use non-Composer Laravel Package and custom libraries inside your Laravel projects.
Using a Git Package
For this purpose, I searched GitHub for a package that is not available on packagist.org and found one.
I’m going to use a package called UniversalForms from wesleytodd.
You can find it here https://github.com/wesleytodd/Universal-Forms-PHP
To use the package, open composer.json file and add it to the require section like this:
"wesleytodd/universal-forms" : "dev-master"
Then under the require section, add a new section called repositories like this:
Finally, run composer update
Now you can add the service provider for the package like any other composer package.
Open config/app.php and add the provider to the provider’s array.
And you can use the package like any other package.
The best thing about this trick is that the repository will be treated like any Composer dependency and will put the package in vendor directory like magic.
Using Private Repositories
As you did with the GitHub repo, you can do the same with your private repos like this:
The only difference here is that you need to install the SSH keys for your git client.
This technique is supported by many git clients like:
If you are using Subversion, it doesn’t have a native idea of branches and tags, so Composer will assume that the code is in $url/branches and $url/tags.
If your repo has a different structure, you can change these value like this:
Autoload Custom Classes or Libraries
Now you can use your non-Composer Laravel packages inside your projects.
What if your package is not even on a repo, maybe a normal PHP library that contains classes or so how to use it inside your Laravel project?
Well, that is so simple. First, create a directory for storing your libraries let’s say app/libraries.
Then include the library file in composer.json file under classmap of autoload section like this:
This will include your file without problems, what if your library has a lot of files?
Great, you can include the directory name instead and Composer will load all of the classes automatically.
Now you can import and use any non-Composer Laravel packages.
I hope you find the post useful. Keep coming back.