7 min read

(For more resources related to this topic, see here.)

Using Bundles

One of the great features in Laravel is the ease in which we can include the class libraries that others have made using bundles. On the Laravel site, there are already many useful bundles, some of which automate certain tasks while others easily integrate with third-party APIs.

A recent addition to the PHP world is Composer, which allows us to use libraries (or packages) that aren’t specific to Laravel.

In this article, we’ll get up-and-running with using bundles, and we’ll even create our own bundle that others can download. We’ll also see how to incorporate Composer into our Laravel installation to open up a wide range of PHP libraries that we can use in our application.

Downloading and installing packages

One of the best features of Laravel is how modular it is. Most of the framework is built using libraries, or packages, that are well tested and widely used in other projects. By using Composer for dependency management, we can easily include other packages and seamlessly integrate them into our Laravel app.

For this recipe, we’ll be installing two popular packages into our app: Jeffrey Way’s Laravel 4 Generators and the Imagine image processing packages.

Getting ready

For this recipe, we need a standard installation of Laravel using Composer.

How to do it…

For this recipe, we will follow these steps:

  1. Go to https://packagist.org/.
  2. In the search box, search for way generator as shown in the following screenshot:

  3. Click on the link for way/generators :

  4. View the details at https://packagist.org/packages/way/generators and take notice of the require line to get the package’s version. For our purposes, we’ll use “way/generators”: “1.0.*” .
  5. In our application’s root directory, open up the composer.json file and add in the package to the require section so it looks like this:

    "require": { "laravel/framework": "4.0.*", "way/generators": "1.0.*" },

  6. Go back to http://packagist.org and perform a search for imagine as shown in the following screenshot:

  7. Click on the link to imagine/imagine and copy the require code for dev-master :

  8. Go back to our composer.json file and update the require section to include the imagine package . It should now look similar to the following code:

    "require": { "laravel/framework": "4.0.*", "way/generators": "1.0.*", "imagine/imagine": "dev-master" },

  9. Open the command line, and in the root of our application, run the Composer update as follows:

    php composer.phar update

  10. Finally, we’ll add the Generator Service Provider, so open the app/config/app.php file and in the providers array, add the following line:


How it works…

To get our package, we first go to packagist.org and search for the package we want. We could also click on the Browse packages link. It will display a list of the most recent packages as well as the most popular. After clicking on the package we want, we’ll be taken to the detail page, which lists various links including the package’s repository and home page. We could also click on the package’s maintainer link to see other packages they have released.

Underneath, we’ll see the various versions of the package. If we open that version’s detail page, we’ll find the code we need to use for our composer.json file. We could either choose to use a strict version number, add a wildcard to the version, or use dev-master, which will install whatever is updated on the package’s master branch. For the Generators package, we’ll only use Version 1.0, but allow any minor fixes to that version. For the imagine package, we’ll use dev-master, so whatever is in their repository’s master branch will be downloaded, regardless of version number.

We then run update on Composer and it will automatically download and install all of the packages we chose. Finally, to use Generators in our app, we need to register the service provider in our app’s config file.

Using the Generators package to set up an app

Generators is a popular Laravel package that automates quite a bit of file creation. In addition to controllers and models, it can also generate views, migrations, seeds, and more, all through a command-line interface.

Getting ready

For this recipe, we’ll be using the Laravel 4 Generators package maintained by Jeffrey Way that was installed in the Downloading and installing packages recipe. We’ll also need a properly configured MySQL database.

How to do it…

Follow these steps for this recipe:

  1. Open the command line in the root of our app and, using the generator, create a scaffold for our cities as follows:

    php artisan generate:scaffold cities --fields="city:string"

  2. In the command line, create a scaffold for our superheroes as follows:

    php artisan generate:scaffold superheroes --fields="name:string,

  3. In our project, look in the app/database/seeds directory and find a file named CitiesTableSeeder.php. Open it and add some data to the $cities array as follows:

    <?php class CitiesTableSeeder extends Seeder { public function run() { DB::table('cities')->delete(); $cities = array( array( 'id' => 1, 'city' => 'New York', 'created_at' => date('Y-m-d g:i:s',time()) ), array( 'id' => 2, 'city' => 'Metropolis', 'created_at' => date('Y-m-d g:i:s',time()) ), array( 'id' => 3, 'city' => 'Gotham', 'created_at' => date('Y-m-d g:i:s',time()) ) ); DB::table('cities')->insert($cities); } }

  4. In the app/database/seeds directory, open SuperheroesTableSeeder.php and add some data to it:

    <?php class SuperheroesTableSeeder extends Seeder { public function run() { DB::table('superheroes')->delete(); $superheroes = array( array( 'name' => 'Spiderman', 'city_id' => 1, 'created_at' => date('Y-m-d g:i:s', time()) ), array( 'name' => 'Superman', 'city_id' => 2, 'created_at' => date('Y-m-d g:i:s', time()) ), array( 'name' => 'Batman', 'city_id' => 3, 'created_at' => date('Y-m-d g:i:s', time()) ), array( 'name' => 'The Thing', 'city_id' => 1, 'created_at' => date('Y-m-d g:i:s', time()) ) ); DB::table('superheroes')->insert($superheroes); } }

  5. In the command line, run the migration then seed the database as follows:

    php artisan migrate php artisan db:seed

  6. Open up a web browser and go to http://{your-server}/cities. We will see our data as shown in the following screenshot:

  7. Now, navigate to http://{your-server}/superheroes and we will see our data as shown in the following screenshot:

How it works…

We begin by running the scaffold generator for our cities and superheroes tables. Using the –fields tag, we can determine which columns we want in our table and also set options such as data type. For our cities table, we’ll only need the name of the city. For our superheroes table, we’ll want the name of the hero as well as the ID of the city where they live.

When we run the generator, many files will automatically be created for us. For example, with cities, we’ll get City.php in our models, CitiesController.php in controllers, and a cities directory in our views with the index, show, create, and edit views. We then get a migration named Create_cities_table.php, a CitiesTableSeeder.php seed file, and CitiesTest.php in our tests directory. We’ll also have our DatabaseSeeder.php file and our routes.php file updated to include everything we need.

To add some data to our tables, we opened the CitiesTableSeeder.php file and updated our $cities array with arrays that represent each row we want to add. We did the same thing for our SuperheroesTableSeeder.php file. Finally, we run the migrations and seeder and our database will be created and all the data will be inserted.

The Generators package has already created the views and controllers we need to manipulate the data, so we can easily go to our browser and see all of our data. We can also create new rows, update existing rows, and delete rows.


Please enter your comment!
Please enter your name here