Add your own github library in Laravel using composer

Adding a random library is quite easy in Laravel. There are two ways you can add an library. Both ways use composer. I am going to use https://github.com/zakir-hyder/oauth-1-lib as our example library. It is basically https://oauth.googlecode.com/svn/code/php/OAuth.php with OAuth namespace and some other changes.

The Easiest Way

Download the oauth-1-lib. Then create folder in app/. Lets name it “library”. So the path to library folder is app/library. Put the src folder of oauth-1-lib in library folder. Rename src folder to “OAuth”. If you want can rename to whatever you want. Then add the library path in the autoload section of composer.json file.

"autoload": {
	"classmap": [
		"app/commands",
		"app/controllers",
		"app/models",
		"app/library",
		"app/database/migrations",
		"app/database/seeds",
		"app/tests/TestCase.php"
	]
},

Then run the dump-autoload command

composer dump-autoload

Now you can use it in you project.

$consumer = new OAuth\OAuthConsumer($consumer_key, $consumer_secret);

The Cool Way

We will composer’s VCS. But before that we have to add a composer.json in the library. We create a composer.json file and add the following.

{
    "name": "zakirhyder/oauth-1-lib",
    "description": "This library includes all the classes needed for OAuth 1/1.0a",
    "license": "MIT",
    "authors": [
        {
            "name": "Zakir Hyder",
            "email": "zakir.hyder@gmail.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "psr-0": {
            "OAuth": "src/"
        }
    }
}

Now lets add our library in a laravel project. First we will add repository laravel’s composer.json

"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/zakir-hyder/oauth-1-lib"
        }
    ],  

Then we add our library in require key

"require": {
		"laravel/framework": "4.1.*",
		"zakirhyder/oauth-1-lib": "dev-master"
	}

One thing about name -I made mistake thinking “zakir-hyder/oauth-1-lib(from the url of https://github.com/zakir-hyder/oauth-1-lib)” is the name I have use. But No I have to use name I used in library’s composer.json.

About Zakir Hyder

This entry was written by .

26. April 2014 by Zakir Hyder
Categories: Composer, Laravel, PHP | Tags: , , , | Comments

Comments

  1. […] Adding a random library is quite easy in Laravel. There are two ways you can add an library. Both ways use composer. I am going to use https://github.com/zakir-hyder/oauth-1-lib as our example library. It is basically https://oauth.googlecode.com/svn/code/php/OAuth.php with OAuth namespace and…  […]

  2. […] we need to add OAuth 1 Lib in our Laravel app. If you go to http://blog.jambura.com/2014/04/26/add-your-own-github-library-in-laravel-using-composer/ then you can see how easy it is to add github in library Laravel app. But If you do not want to […]