Install and Use Non-Composer Laravel Packages

If you want to use a package in Laravel, you simply add a single line in the 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? May be 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:

  "repositories": [
    {
      "type": "vcs",

      "url": "https://github.com/wesleytodd/Universal-Forms-PHP"

    }
  ]

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.

'Wesleytodd\UniversalForms\Drivers\Laravel\UniversalFormsServiceProvider',

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:

{
  "require": {
    "likegeeks/my-repo": "dev-master"
  },
  "repositories": [
    {
      "type": "vcs",
      "url":  "git@bitbucket.org:likegeeks/my-repo.git"
    }
  ]
}

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:

  • Git
  • Subversion
  • Mercurial

 

Using Subversion

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/tags

.

If your repo has a different structure, you can change these value like this:

{
  "repositories": [
    {
      "type": "vcs",
      "url": "http://svn.website.com/projectA/",
      "trunk-path": "MyTrunk",
      "branches-path": "MyBranches",
      "tags-path": "MyTags"
    }
  ]
}

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:

{
  "autoload": {
    "classmap": [
      "app/libraries/myLib.php"
    ]
  }
}

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.

Thank you.

4 thoughts on “Install and Use Non-Composer Laravel Packages
  1. looked for some days to include my own code in laravel and finally got there with your last method proposed ! Thanks a lot !

    1. You’re welcome!
      Yout right, but for me, I was transferring my old to MVC (which is not so easy) and adjust it to Laravel. I felt much better when modifying it back.

Leave a Reply

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