Laravel 4

Package Management with Composer Made Easy

Laravel & Composer: perfect marriage

I am slowly preparing to refactor some ancient PHP code and have recently been toying with Laravel—a PHP framework for “web artisans”—to see how it compares to other frameworks out there. It has been a pleasant experience so far and I really appreciate how easy it is to add/maintain third-party code.

The framework seamlessly integrates with Composer, which is a powerful PHP packaging system that makes code sharing and dependency management a hassle-free experience. Each module in Laravel is a Composer package. Updating core modules, adding and maintaining third-party libraries is a matter of few clicks/commands. I will quickly demonstrate it by installing HybridAuth, a PHP library for authentication through social networks.

N.B. I assume that you know how to install Laravel 4 and Composer, obtain API keys, as well as the basics of Laravel routing/etc. If not, do check the additional resources section below for some helpful links.

  • Start by editing composer.json and specifying that you need the latest version (denoted by *) of the HybridAuth package (hybridauth/hybridauth). By default Composer will search for the package in the Packagist repository:

  • {
    	...
    	"require": {
    		...
    		"hybridauth/hybridauth": "*"
    	},
    	...
    }
    
    
  • Now run php composer.phar update (or composer update, depending on how your system is configured) to fetch and install the package.

  • Create a HybridAuth configuration file (app/config/hybridauth.php) and specify your social networks' credentials:

    <?php
    return array(	
    	"base_url"   => "http://URL/social/auth",
    	"providers"  => array (
    		"Google"     => array (
    			"enabled"    => true,
    			"keys"       => array ( "id" => "ID", "secret" => "SECRET" ),
    			),
    		"Facebook"   => array (
    			"enabled"    => true,
    			"keys"       => array ( "id" => "ID", "secret" => "SECRET" ),
    			),
    		"Twitter"    => array (
    			"enabled"    => true,
    			"keys"       => array ( "key" => "ID", "secret" => "SECRET" )
    			)
    	),
    );
    
    
  • Edit your application's Routes file (app/routes.php) with the following code:

    Route::get('social/{action?}', array("as" => "hybridauth", function($action = "")
    {
    	// check URL segment
    	if ($action == "auth") {
    		// process authentication
    		try {
    			Hybrid_Endpoint::process();
    		}
    		catch (Exception $e) {
    			// redirect back to http://URL/social/
    			return Redirect::route('hybridauth');
    		}
    		return;
    	}
    	try {
    		// create a HybridAuth object
    		$socialAuth = new Hybrid_Auth(app_path() . '/config/hybridauth.php');
    		// authenticate with Google
    		$provider = $socialAuth->authenticate("google");
    		// fetch user profile
    		$userProfile = $provider->getUserProfile();
    	}
    	catch(Exception $e) {
    		// exception codes can be found on HybridAuth's web site
    		return $e->getMessage();
    	}
    	// access user profile data
    	echo "Connected with: <b>{$provider->id}</b><br />";
    	echo "As: <b>{$userProfile->displayName}</b><br />";
    	echo "<pre>" . print_r( $userProfile, true ) . "</pre><br />";
    
    	// logout
    	$provider->logout();
    }));

That's pretty much it. Just a few lines of code get you a social network authentication module up and running at http://URL/social/. I do suggest that you move all logic to Controller/Model and keep your Routes lean.

Additional Resources

Follows are some useful resources to get you started with Laravel 4: