Creating a Composer package in Laravel

Using Laravel’s Workbench, we can easily create a package that can be used and installed by
Composer. We can also add functionality so that the package integrates seamlessly into our
Laravel app. In this recipe, we’ll be creating a simple package that will display a list of Vimeo
videos for a specified user.

Getting ready

For this recipe, we’ll need a standard Laravel installation.

How to do…

To complete this recipe, follow these steps:
1. In the app/config directory, open the workbench.php file and update it with the
following information:
<?php
return array(
‘name’ => ‘Terry Matula’,
’email’ => ‘terrymatula@gmail.com’,
);
2. In the command line, use artisan to set up our package:
php artisan workbench matula/vimeolist –resources
3. Find the directory that will hold our source files and create a file named Vimeolist.
php. In this example, we would put the file in workbench/matula/vimeolist/
src/Matula/Vimeolist/:
<?php namespace Matula\Vimeolist;
class Vimeolist
{
private $base_url =
‘http://vimeo.com/api/v2/{username}/videos.json’;
private $username;
public function __construct($username = ‘userscape’) {
$this->setUser($username);
return $this;
}
/**

/**
* Set the username for our list
*
* @return void
*/
public function setUser($username = NULL) {
$this->username = is_null($username) ? $this-
>username : urlencode($username);
return $this;
}

/**
* Set up the url and get the contents
*
* @return json
*/
private function getFeed() {
$url = str_replace(‘{username}’, $this->username,
$this->base_url);
$feed = file_get_contents($url);
return $feed;
}
/**
* Turn the feed into an object
*
* @return object
*/
public function parseFeed() {
$json = $this->getFeed();
$object = json_decode($json);
return $object;
}
/**
* Get the list and format the return
*
* @return array
*/
public function getList() {
$list = array();
$posts = $this->parseFeed();
foreach ($posts as $post) {
$list[$post->id][‘title’] = $post->title;
$list[$post->id][‘url’] = $post->url;
$list[$post->id][‘description’] = $post-
>description;
$list[$post->id][‘thumbnail’] = $post-
>thumbnail_small;
}
return $list;
}
}

4. In the same directory as the file we just created, open the file named
VimeolistServiceProvider.php and update it:
<?php namespace Matula\Vimeolist;
use Illuminate\Support\ServiceProvider;
class VimeolistServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->package(‘matula/vimeolist’);
}
/**
* Register the service provider.
*
* @return void
*/
public function register()

{
$this->app[‘vimeolist’] = $this->app-
>share(function($app)
{
return new Vimeolist;
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array(‘vimeolist’);
}
}

5. Open the app.php file in the app/config directory, and in the providers array,
add our service provider as follows:
‘Matula\Vimeolist\VimeolistServiceProvider’,
6. In the command line, run the following command:
php composer.phar dump-autoload
7. In routes.php, add a route to display the data as follows:
Route::get(‘vimeo/{username?}’, function($username = null) use
($app)
{
$vimeo = $app[‘vimeolist’];
if ($username) {
$vimeo->setUser($username);
}
dd($vimeo->getList());
});

How it works

Our first step is to update our workbench’s configuration file to hold our name and e-mail
address. This will then be used for any other packages that we create in Laravel.
Next, we run the artisan command to create the files we need for our package. By using
the –resources flag, it will also generate other files and directories that can be used
specifically for Laravel. Once it’s completed, there will be a new folder in our workbench
directory that holds all our package’s files. After drilling down into the directories, we’ll get
to a directory that holds our service provider file, and in this directory, we’ll add our class file.
This example class will simply get a list of videos for a user from the Vimeo API. We have
methods that will allow us to set a username, get the contents of the API endpoint, turn the
JSON into a PHP object, and then create and return a formatted array. As a best practice, we
should also make sure our code is tested and that we can put those files in the test directory.
To better integrate with Laravel, we need to update the service provider. We first update
the register method and set the name we want to pass to Laravel’s app variable and
then we update the provides method to return the package name. Next, we need to update
our app configuration file to actually register the service provider. Then, once we run the
dump-autoload command in Composer, our new package will be available to use.
Finally, we create a route to interact with the package. We’ll have one optional parameter, that
is, the username. We also need to make sure the $app variable is available in our route. Then,
when we call $app[‘vimeolist’], the service provider will automatically instantiate our
class and allow us to access the Vimeo list. For our purposes, we’re only using the dd() helper
function of Laravel to display the data, but we could also pass it to a view and make it look nicer.

 

Leave a Reply

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