1. Code
  2. PHP
  3. Laravel

Combining Laravel 4 and Backbone

Scroll to top
60+ min read

For this tutorial, we're going to be building a single page app using Laravel 4 and Backbone.js. Both frameworks make it very easy to use a different templating engine other than their respective default, so we're going to use Mustache, which is an engine that is common to both. By using the same templating language on both sides of our application, we'll be able to share our views betweem them, saving us from having to repeat our work multiple times.

Our Backbone app will be powered by a Laravel 4 JSON API which we'll develop together. Laravel 4 comes with some new features that make the development of this API very easy. I'll show you a few tricks along the way to allow you to stay a bit more organized.

All of our dependencies will be managed by Package Managers, there will be no manual downloading or updating of libraries for this application! In addition, I'll be showing you how to leverage a little extra power from some of our dependencies.

For this project we'll be using:

To complete this tutorial, you'll need the following items installed:

  • Composer: You can download this from the homepage, I recommend the global install instructions located here.
  • Node + NPM: the installer on the homepage will install both items.
  • LESS Compiler: If you're on a Mac, I recommend CodeKit. However, regardless of your operating system, or if you do not feel like paying for CodeKit, you can just install the LESS Compiler for Node.js by typing npm install -g less at the command prompt.

Part 1: The Base Architecture

First things first, we need to get our application setup before we can begin adding our business logic to it. We'll do a basic setup of Laravel 4 and get all of our dependencies installed using our Package Managers.

Git

Let's start by creating a git repository to work in. For your reference, this entire repo will be made publicly available at https://github.com/conarwelsh/nettuts-laravel4-and-backbone.

1
mkdir project && cd project
2
git init

Laravel 4 Installation

Laravel 4 uses Composer to install all of its dependencies, but first we'll need an application structure to install into. The "develop" branch on Laravel's Github repository is the home for this application structure. However, at the time of writing this article, Laravel 4 was still in beta, so I needed to be prepared for this structure to change at any time. By adding Laravel as a remote repository, we can pull in these changes whenever we need to. In fact, while something is in beta-mode, it's a good practice to run these commands after each composer update. However, Laravel 4 is now the newest, stable version.

1
git remote add laravel https://github.com/laravel/laravel
2
git fetch laravel
3
git merge laravel/develop
4
git add . && git commit -am "commit the laravel application structure"

So we have the application structure, but all of the library files that Laravel needs are not yet installed. You'll notice at the root of our application there's a file called composer.json. This is the file that will keep track of all the dependencies that our Laravel application requires. Before we tell Composer to download and install them, let's first add a few more dependencies that we're going to need. We'll be adding:

  • Jeffrey Way's Generators: Some very useful commands to greatly improve our workflow by automatically generating file stubs for us.
  • Laravel 4 Mustache: This will allow us to seamlessly use Mustache.php in our Laravel project, just as we would Blade.
  • Twitter Bootstrap: We'll use the LESS files from this project to speed up our front-end development.
  • PHPUnit: We'll be doing some TDD for our JSON API, PHPUnit will be our testing engine.
  • Mockery: Mockery will help us "mock" objects during our testing.

PHPUnit and Mockery are only required in our development environment, so we'll specify that in our composer.json file.


composer.json

1
{
2
  "require": {
3
    "laravel/framework": "4.0.*",
4
    "way/generators": "dev-master",
5
    "twitter/bootstrap": "dev-master",
6
    "conarwelsh/mustache-l4": "dev-master"
7
  },
8
  "require-dev": {
9
    "phpunit/phpunit": "3.7.*",
10
    "mockery/mockery": "0.7.*"
11
  },
12
  "autoload": {
13
    "classmap": [
14
      "app/commands",
15
      "app/controllers",
16
      "app/models",
17
      "app/database/migrations",
18
      "app/database/seeds",
19
      "app/tests/TestCase.php"
20
    ]
21
  },
22
  "scripts": {
23
    "post-update-cmd": "php artisan optimize"
24
  },
25
  "minimum-stability": "dev"
26
}

Now we just need to tell Composer to do all of our leg work! Below, notice the --dev switch, we're telling composer that we're in our development environment and that it should also install all of our dependencies listed in "require-dev".

1
composer install --dev

After that finishes installing, we'll need to inform Laravel of a few of our dependencies. Laravel uses "service providers" for this purpose. These service providers basically just tell Laravel how their code is going to interact with the application and to run any necessary setup procedures. Open up app/config/app.php and add the following two items to the "providers" array. Not all packages require this, only those that will enhance or change the functionality of Laravel.


app/config/app.php

1
...
2
3
'Way\Generators\GeneratorsServiceProvider',
4
'Conarwelsh\MustacheL4\MustacheL4ServiceProvider',
5
6
...

Lastly, we just need to do some generic application tweaks to complete our Laravel installation. Let's open up bootstrap/start.php and tell Laravel our machine name so that it can determine what environment it's in.


bootstrap/start.php

1
/*

2
|--------------------------------------------------------------------------

3
| Detect The Application Environment

4
|--------------------------------------------------------------------------

5
|

6
| Laravel takes a dead simple approach to your application environments

7
| so you can just specify a machine name or HTTP host that matches a

8
| given environment, then we will automatically detect it for you.

9
|

10
*/
11
12
$env = $app->detectEnvironment(array(
13
14
  'local' => array('your-machine-name'),
15
16
));

Replace "your-machine-name" with whatever the hostname for your machine is. If you are unsure of what your exact machine name is, you can just type hostname at the command prompt (on Mac or Linux), whatever it prints out is the value that belongs in this setting.

We want our views to be able to be served to our client from a web request. Currently, our views are stored outside of our public folder, which would mean that they are not publicly accessible. Luckily, Laravel makes it very easy to move or add other view folders. Open up app/config/view.php and change the paths setting to point to our public folder. This setting works like the PHP native include path, it will check in each folder until it finds a matching view file, so feel free to add several here:


app/config/view.php

1
'paths' => array(__DIR__.'/../../public/views'),

Next you will need to configure your database. Open up app/config/database.php and add in your database settings.

Note: It is recommended to use 127.0.0.1 instead of localhost. You get a bit of a performance boost on most systems, and with some system configurations, localhost will not even connect properly.

Finally, you just need to make sure that your storage folder is writable.

1
chmod -R 755 app/storage

Laravel is now installed, with all of its dependencies, as well as our own dependencies. Now let's setup our Backbone installation!

Just like our composer.json installed all of our server-side dependencies, we'll create a package.json in our public folder to install all of our client-side dependencies.

For our client-side dependencies we'll use:

  • Underscore.js: This is a dependency of Backbone.js, and a handy toolbelt of functions.
  • Backbone.js: This is our client-side MVC that we'll use to build out our application.
  • Mustache.js: The Javascript version of our templating library, by using the same templating language both on the client and the server, we can share views, as opposed to duplicating logic.

public/package.json

1
{
2
  "name": "nettuts-laravel4-and-backbone",
3
  "version": "0.0.1",
4
  "private": true,
5
  "dependencies": {
6
    "underscore": "*",
7
    "backbone": "*",
8
    "mustache": "*"
9
  }
10
}

Now just switch into your public folder, and run npm install. After that completes, lets switch back to our application root so we're prepared for the rest of our commands.

1
cd public
2
npm install
3
cd ..

Package managers save us from a ton of work, should you want to update any of these libraries, all you have to do is run npm update or composer update. Also, should you want to lock any of these libraries in at a specific version, all you have to do is specify the version number, and the package manager will handle the rest.

To wrap up our setup process we'll just add in all of the basic project files and folders that we'll need, and then test it out to ensure it all works as expected.

We'll need to add the following folders:

  • public/views
  • public/views/layouts
  • public/js
  • public/css

And the following files:

  • public/css/styles.less
  • public/js/app.js
  • public/views/app.mustache

To accomplish this, we can use a one-liner:

1
mkdir public/views public/views/layouts public/js public/css && touch public/css/styles.less public/js/app.js public/views/app.mustache

Twitter Bootstrap also has two JavaScript dependencies that we'll need, so let's just copy them from the vendor folder into our public folder. They are:

  • html5shiv.js: allows us to use HTML5 elements without fear of older browsers not supporting them
  • bootstrap.min.js: the supporting JavaScript libraries for Twitter Bootstrap
1
cp vendor/twitter/bootstrap/docs/assets/js/html5shiv.js public/js/html5shiv.js
2
cp vendor/twitter/bootstrap/docs/assets/js/bootstrap.min.js public/js/bootstrap.min.js

For our layout file, the Twitter Bootstrap also provides us with some nice starter templates to work with, so let's copy one into our layouts folder for a head start:

1
cp vendor/twitter/bootstrap/docs/examples/starter-template.html public/views/layouts/application.blade.php

Notice that I am using a blade extension here, this could just as easily be a mustache template, but I wanted to show you how easy it is to mix the templating engines. Since our layout will be rendered on page load, and will not need to be re-rendered by the client, we are safe to use PHP here exclusively. If for some reason you find yourself needing to render this file on the client-side, you would want to switch this file to use the Mustache templating engine instead.

Now that we have all of our basic files in place, let's add some starter content that we can use to test that everything is working as we would expect. I'm providing you with some basic stubs to get you started.


public/css/styles.less

We'll just import the Twitter Bootstrap files from the vendor directory as opposed to copying them. This allows us to update Twitter Bootstrap with nothing but a composer update.

We declare our variables at the end of the file, the LESS compiler will figure out the value of all of its variables before parsing the LESS into CSS. This means that by re-defining a Twitter Bootstrap variable at the end of the file, the value will actually change for all of the files included, allowing us to do simple overrides without modifying the Twitter Bootstrap core files.

1
/**

2
 * Import Twitter Bootstrap Base File

3
 ******************************************************************************************

4
 */
5
@import "../../vendor/twitter/bootstrap/less/bootstrap";
6
7
8
/**

9
 * Define App Styles

10
 * Do this before the responsive include, so that it can override properly as needed.

11
 ******************************************************************************************

12
 */
13
body {
14
  padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
15
}
16
17
/* this will set the position of our alerts */
18
#notifications {
19
  width: 300px;
20
  position: fixed;
21
  top: 50px;
22
  left: 50%;
23
  margin-left: -150px;
24
  text-align: center;
25
}
26
27
/**

28
 * Import Bootstrap's Responsive Overrides

29
 * now we allow bootstrap to set the overrides for a responsive layout

30
 ******************************************************************************************

31
 */
32
@import "../../vendor/twitter/bootstrap/less/responsive";
33
34
35
/**

36
 * Define our variables last, any variable declared here will be used in the includes above

37
 * which means that we can override any of the variables used in the bootstrap files easily

38
 * without modifying any of the core bootstrap files

39
 ******************************************************************************************

40
 */
41
42
// Scaffolding
43
// -------------------------
44
@bodyBackground:    #f2f2f2;
45
@textColor:       #575757;
46
47
// Links
48
// -------------------------
49
@linkColor:       #41a096;
50
51
// Typography
52
// -------------------------
53
@sansFontFamily:    Arial, Helvetica, sans-serif;

public/js/app.js

Now we'll wrap all of our code in an immediately-invoking-anonymous-function that passes in a few global objects. We'll then alias these global objects to something more useful to us. Also, we'll cache a few jQuery objects inside the document ready function.

1
//alias the global object

2
//alias jQuery so we can potentially use other libraries that utilize $

3
//alias Backbone to save us on some typing

4
(function(exports, $, bb){
5
6
  //document ready

7
  $(function(){
8
9
    /**

10
     ***************************************

11
     * Cached Globals

12
     ***************************************

13
     */
14
    var $window, $body, $document;
15
16
    $window  = $(window);
17
    $document = $(document);
18
    $body   = $('body');
19
20
21
  });//end document ready

22
23
}(this, jQuery, Backbone));

public/views/layouts/application.blade.php

Next is just a simple HTML layout file. We're however using the asset helper from Laravel to aid us in creating paths to our assets. It is good practice to use this type of helper, because if you ever happen to move your project into a sub-folder, all of your links will still work.

We made sure that we included all of our dependencies in this file, and also added the jQuery dependency. I chose to request jQuery from the Google CDN, because chances are the visiting user of this site will already have a copy from that CDN cached in their browser, saving us from having to complete the HTTP request for it.

One important thing to note here is the way in which we are nesting our view. Mustache does not have Block Sections like Blade does, so instead, the contents of the nested view will be made available under a variable with the name of the section. I will point this out when we render this view from our route.

1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
 <meta charset="utf-8">
5
 <title>Laravel4 & Backbone | Nettuts</title>
6
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
 <meta name="description" content="A single page blog built using Backbone.js, Laravel, and Twitter Bootstrap">
8
 <meta name="author" content="Conar Welsh">
9
10
 <link href="{{ asset('css/styles.css') }}" rel="stylesheet">
11
12
 <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
13
 <!--[if lt IE 9]>

14
 <script src="{{ asset('js/html5shiv.js') }}"></script>

15
 <![endif]-->
16
</head>
17
<body>
18
19
 <div id="notifications">
20
 </div>
21
22
 <div class="navbar navbar-inverse navbar-fixed-top">
23
  <div class="navbar-inner">
24
   <div class="container">
25
    <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
26
     <span class="icon-bar"></span>
27
     <span class="icon-bar"></span>
28
     <span class="icon-bar"></span>
29
    </button>
30
    <a class="brand" href="#">Nettuts Tutorial</a>
31
    <div class="nav-collapse collapse">
32
     <ul class="nav">
33
      <li class="active"><a href="#">Blog</a></li>
34
     </ul>
35
    </div><!--/.nav-collapse -->
36
   </div>
37
  </div>
38
 </div>
39
40
 <div class="container" data-role="main">
41
  {{--since we are using mustache as the view, it does not have a concept of sections like blade has, so instead of using @yield here, our nested view will just be a variable that we can echo--}}
42
43
  {{ $content }}
44
45
 </div> <!-- /container -->
46
47
 <!-- Placed at the end of the document so the pages load faster -->
48
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <!-- use Google CDN for jQuery to hopefully get a cached copy -->
49
 <script src="{{ asset('node_modules/underscore/underscore-min.js') }}"></script>
50
 <script src="{{ asset('node_modules/backbone/backbone-min.js') }}"></script>
51
 <script src="{{ asset('node_modules/mustache/mustache.js') }}"></script>
52
 <script src="{{ asset('js/bootstrap.min.js') }}"></script>
53
 <script src="{{ asset('js/app.js') }}"></script>
54
 @yield('scripts')
55
</body>
56
</html>

public/views/app.mustache

Next is just a simple view that we'll nest into our layout.

1
<dl>
2
  <dt>Q. What did Biggie say when he watched inception?</dt>
3
  <dd>A. "It was all a dream!"</dd>
4
</dl>

app/routes.php

Laravel should have already provided you with a default route, all we're doing here is changing the name of the view which that route is going to render.

Remember from above, I told you that the nested view was going to be available under a variable named whatever the parent section was? Well, when you nest a view, the first parameter to the function is the section name:

1
View::make('view.path')->nest($sectionName, $nestedViewPath, $viewVariables);

In our nest command we called the section "content", that means if we echo $content from our layout, we'll get the rendered contents of that view. If we were to do return View::make('layouts.application')->nest('foobar', 'app'); then our nested view would be available under a variable named $foobar.

1
<?php
2
3
//backbone app route

4
Route::get('/', function()
5
{
6
  //change our view name to the view we created in a previous step

7
  //notice that we do not need to provide the .mustache extension

8
  return View::make('layouts.application')->nest('content', 'app');
9
});

With all of our basic files in place, we can test to ensure everything went OK. Laravel 4 utilizes the new PHP web server to provide us with a great little development environment. So long to the days of having a million virtual hosts setup on your development machine for every project that you work on!

Note: make sure that you've compiled your LESS file first!

1
php artisan serve

If you followed along correctly, you should be laughing hysterically at my horrible sense of humor, and all of our assets should be properly included into the page.


Part 2: Laravel 4 JSON API

Now we'll build the API that will power our Backbone application. Laravel 4 makes this process a breeze.

API Guidelines

First let's go over a few general guidelines to keep in mind while we build our API:

  • Status Codes: Responses should reply with proper status codes, fight the temptation to just place an { error: "this is an error message" } in the body of your response. Use the HTTP protocol to its fullest!

    • 200: success
    • 201: resource created
    • 204: success, but no content to return
    • 400: request not fulfilled //validation error
    • 401: not authenticated
    • 403: refusal to respond //wrong credentials, do not have permission (un-owned resource)
    • 404: not found
    • 500: other error
  • Resource Methods: Even though controllers will be serving different resources, they should still have very similar behavior. The more predictable your API is, the easier it is to implement and adopt.

    • index: Return a collection of resources.
    • show: Return a single resource.
    • create: Return a form. This form should detail out the required fields, validation, and labels as best as possible. As well as anything else needed to properly create a resource. Even though this is a JSON API, it is very useful to return a form here. Both a computer and a person can parse through this form, and very easily decipher which items are needed to fill out this form successsfully. This is a very easy way to "document" the needs of your API.
    • store: Store a new resource and return with the proper status code: 201.
    • edit: Return a form filled with the current state of a resource. This form should detail out the required fields, validation, and labels as best as possible. As well as anything else needed to properly edit a resource.
    • update: Update an existing resource and return with the proper status code.
    • delete: Delete an existing resource and return with the proper status code: 204.

Routing & Versioning

API's are designed to be around for a while. This is not like your website where you can just change its functionality at the drop of a dime. If you have programs that use your API, they are not going to be happy with you if you change things around and their program breaks. For this reason, it's important that you use versioning.

We can always create a "version two" with additional, or altered functionality, and allow our subscribing programs to opt-in to these changes, rather than be forced.

Laravel provides us with route groups that are perfect for this, place the following code ABOVE our first route:

1
<?php
2
3
//create a group of routes that will belong to APIv1

4
Route::group(array('prefix' => 'v1'), function()
5
{
6
  //... insert API routes here...

7
});

Generating Resources

We're going to use Jeffrey Way's generators to generate our resources. When we generate a resource, it will create the following items for us:

  • Controller
  • Model
  • Views (index.blade.php, show.blade.php, create.blade.php, edit.blade.php)
  • Migration
  • Seeds

We're only going to need two resources for this app: a Post resource and a Comment resource.

Note: in a recent update to the generators, I have been receiving a permissions error due to the way my web servers are setup. To remedy this problem, you must allow write permissions to the folder that the generators write the temp file to.

1
sudo chmod -R 755 vendor/way/generators/src/Way/

Run the generate:resource command

1
php artisan generate:resource post --fields="title:string, content:text, author_name:string"
2
3
php artisan generate:resource comment --fields="content:text, author_name:string, post_id:integer"

You should now pause for a second to investigate all of the files that the generator created for us.

Adjust the Generated Resources

The generate:resource command saved us a lot of work, but due to our unique configuration, we're still going to need to make some modifications.

First of all, the generator placed the views it created in the app/views folder, so we need to move them to the public/views folder

1
mv app/views/posts public/views/posts
2
mv app/views/comments public/views/comments

app/routes.php

We decided that we wanted our API to be versioned, so we'll need to move the routes that the generator created for us into the version group. We'll also want to namespace our controllers with the corresponding version, so that we can have a different set of controllers for each version we build. Also the comments resource needs to be nested under the posts resource.

1
<?php
2
3
//create a group of routes that will belong to APIv1

4
Route::group(array('prefix' => 'v1'), function()
5
{
6
  //... insert API routes here...

7
  Route::resource('posts', 'V1\PostsController'); //notice the namespace

8
  Route::resource('posts.comments', 'V1\PostsCommentsController'); //notice the namespace, and the nesting

9
});
10
11
//backbone app route

12
Route::get('/', function()
13
{
14
  //change our view name to the view we created in a previous step

15
  //notice that we do not need to provide the .mustache extension

16
  return View::make('layouts.application')->nest('content', 'app');
17
});

Since we namespaced our controllers, we should move them into their own folder for organization, let's create a folder named V1 and move our generated controllers into it. Also, since we nested our comments controller under the posts controller, let's change the name of that controller to reflect the relationship.

1
mkdir app/controllers/V1
2
mv app/controllers/PostsController.php app/controllers/V1/
3
mv app/controllers/CommentsController.php app/controllers/V1/PostsCommentsController.php

We'll need to update the controller files to reflect our changes as well. First of all, we need to namespace them, and since they are namespaced, any classes outside of that namespace will need to be manually imported with the use statement.

app/controllers/PostsController.php

1
<?php
2
//use our new namespace

3
namespace V1;
4
5
//import classes that are not in this new namespace

6
use BaseController;
7
8
class PostsController extends BaseController {

app/controllers/PostsCommentsController.php

We also need to update our CommentsController with our new name: PostsCommentsController

1
<?php
2
//use our new namespace

3
namespace V1;
4
5
//import classes that are not in this new namespace

6
use BaseController;
7
8
//rename our controller class

9
class PostsCommentsController extends BaseController {

Adding in Repositories

By default, repositories are not part of Laravel. Laravel is extremely flexible though, and makes it very easy to add them in. We're going to use repositories to help us separate our logic for code re-usability, as well as for testing. For now we'll just get setup to use repositories, we'll add in the proper logic later.

Let's make a folder to store our repositories in:

1
mkdir app/repositories

To let our auto-loader know about this new folder, we need to add it to our composer.json file. Take a look at the updated "autoload" section of our file, and you'll see that we added in the repositories folder.

composer.json

1
{
2
  "require": {
3
    "laravel/framework": "4.0.*",
4
    "way/generators": "dev-master",
5
    "twitter/bootstrap": "dev-master",
6
    "conarwelsh/mustache-l4": "dev-master"
7
  },
8
  "require-dev": {
9
    "phpunit/phpunit": "3.7.*",
10
    "mockery/mockery": "0.7.*"
11
  },
12
  "autoload": {
13
    "classmap": [
14
      "app/commands",
15
      "app/controllers",
16
      "app/models",
17
      "app/database/migrations",
18
      "app/database/seeds",
19
      "app/tests/TestCase.php",
20
      "app/repositories"
21
    ]
22
  },
23
  "scripts": {
24
    "post-update-cmd": "php artisan optimize"
25
  },
26
  "minimum-stability": "dev"
27
}

Seeding Our Database

Database seeds are a useful tool, they provide us with an easy way to fill our database with some content. The generators provided us with base files for seeding, we merely need to add in some actual seeds.

app/database/seeds/PostsTableSeeder.php

1
<?php
2
3
class PostsTableSeeder extends Seeder {
4
5
  public function run()
6
  {
7
    $posts = array(
8
      array(
9
        'title'    => 'Test Post',
10
        'content'   => 'Lorem ipsum Reprehenderit velit est irure in enim in magna aute occaecat qui velit ad.',
11
        'author_name' => 'Conar Welsh',
12
        'created_at' => date('Y-m-d H:i:s'),
13
        'updated_at' => date('Y-m-d H:i:s'),
14
      ),
15
      array(
16
        'title'    => 'Another Test Post',
17
        'content'   => 'Lorem ipsum Reprehenderit velit est irure in enim in magna aute occaecat qui velit ad.',
18
        'author_name' => 'Conar Welsh',
19
        'created_at' => date('Y-m-d H:i:s'),
20
        'updated_at' => date('Y-m-d H:i:s'),
21
      ),
22
    );
23
24
    // Uncomment the below to run the seeder

25
    DB::table('posts')->insert($posts);
26
  }
27
28
}

app/database/seeds/CommentsTableSeeder.php

1
<?php
2
3
class CommentsTableSeeder extends Seeder {
4
5
  public function run()
6
  {
7
    $comments = array(
8
      array(
9
        'content'   => 'Lorem ipsum Nisi dolore ut incididunt mollit tempor proident eu velit cillum dolore sed',
10
        'author_name' => 'Testy McTesterson',
11
        'post_id'   => 1,
12
        'created_at' => date('Y-m-d H:i:s'),
13
        'updated_at' => date('Y-m-d H:i:s'),
14
      ),
15
      array(
16
        'content'   => 'Lorem ipsum Nisi dolore ut incididunt mollit tempor proident eu velit cillum dolore sed',
17
        'author_name' => 'Testy McTesterson',
18
        'post_id'   => 1,
19
        'created_at' => date('Y-m-d H:i:s'),
20
        'updated_at' => date('Y-m-d H:i:s'),
21
      ),
22
      array(
23
        'content'   => 'Lorem ipsum Nisi dolore ut incididunt mollit tempor proident eu velit cillum dolore sed',
24
        'author_name' => 'Testy McTesterson',
25
        'post_id'   => 2,
26
        'created_at' => date('Y-m-d H:i:s'),
27
        'updated_at' => date('Y-m-d H:i:s'),
28
      ),
29
    );
30
31
    // Uncomment the below to run the seeder

32
    DB::table('comments')->insert($comments);
33
  }
34
35
}

Don't forget to run composer dump-autoload to let the Composer auto loader know about the new migration files!

1
composer dump-autoload

Now we can run our migrations and seed the database. Laravel provides us with a single command to do both:

1
php artisan migrate --seed

Tests

Testing is one of those topics in development that no one can argue the importance of, however most people tend to ignore it due to the learning curve. Testing is really not that difficult and it can dramatically improve your application. For this tutorial, we'll setup some basic tests to help us ensure that our API is functioning properly. We'll build this API TDD style. The rules of TDD state that we are not allowed to write any production code until we have failing tests that warrants it. However, if I were to walk you through each test individually, this would prove to be a very long tutorial, so in the interest of brevity, I will just provide you with some tests to work from, and then the correct code to make those tests pass afterwards.

Before we write any tests though, we should first check the current test status of our application. Since we installed PHPUnit via composer, we have the binaries available to us to use. All you need to do is run:

1
vendor/phpunit/phpunit/phpunit.php

Whoops! We already have a failure! The test that is failing is actually an example test that comes pre-installed in our Laravel application structure, this tests against the default route that was also installed with the Laravel application structure. Since we modified this route, we cannot be surprised that the test failed. We can however, just delete this test altogether as it does not apply to our application.

1
rm app/tests/ExampleTest.php

If you run the PHPUnit command again, you will see that no tests were executed, and we have a clean slate for testing.

Note: it is possible that if you have an older version of Jeffrey Way's generators that you'll actually have a few tests in there that were created by those generators, and those tests are probably failing. Just delete or overwrite those tests with the ones found below to proceed.

For this tutorial we'll be testing our controllers and our repositories. Let's create a few folders to store these tests in:

1
mkdir app/tests/controllers app/tests/repositories

Now for the test files. We're going to use Mockery to mock our repositories for our controller tests. Mockery objects do as their name implies, they "mock" objects and report back to us on how those objects were interacted with.

In the case of the controller tests, we do not actually want the repositories to be called, after all, these are the controller tests, not the repository tests. So Mockery will set us up objects to use instead of our repositories, and let us know whether or not those objects were called as we expected them to.

In order to pull this off, we'll have to tell the controllers to use our "mocked" objects as opposed to the real things. We'll just tell our Application to use a mocked instance next time a certain class is requested. The command looks like this:

1
App::instance($classToReplace, $instanceOfClassToReplaceWith);

The overall mocking process will go something like this:

  • Create a new Mockery object, providing it the name of the class which it is to mock.
  • Tell the Mockery object which methods it should expect to receive, how many times it should receive that method, and what that method should return.
  • Use the command shown above to tell our Application to use this new Mockery object instead of the default.
  • Run the controller method like usual.
  • Assert the response.

app/tests/controllers/CommentsControllerTest.php

1
<?php
2
3
class CommentsControllerTest extends TestCase {
4
5
  /**

6
   ************************************************************************

7
   * Basic Route Tests

8
   * notice that we can use our route() helper here!

9
   ************************************************************************

10
   */
11
12
  //test that GET /v1/posts/1/comments returns HTTP 200

13
  public function testIndex()
14
  {
15
    $response = $this->call('GET', route('v1.posts.comments.index', array(1)) );
16
    $this->assertTrue($response->isOk());
17
  }
18
19
  //test that GET /v1/posts/1/comments/1 returns HTTP 200

20
  public function testShow()
21
  {
22
    $response = $this->call('GET', route('v1.posts.comments.show', array(1,1)) );
23
    $this->assertTrue($response->isOk());
24
  }
25
26
  //test that GET /v1/posts/1/comments/create returns HTTP 200

27
  public function testCreate()
28
  {
29
    $response = $this->call('GET', route('v1.posts.comments.create', array(1)) );
30
    $this->assertTrue($response->isOk());
31
  }
32
33
  //test that GET /v1/posts/1/comments/1/edit returns HTTP 200

34
  public function testEdit()
35
  {
36
    $response = $this->call('GET', route('v1.posts.comments.edit', array(1,1)) );
37
    $this->assertTrue($response->isOk());
38
  }
39
40
  /**

41
   *************************************************************************

42
   * Tests to ensure that the controller calls the repo as we expect

43
   * notice we are "Mocking" our repository

44
   *

45
   * also notice that we do not really care about the data or interactions

46
   * we merely care that the controller is doing what we are going to want

47
   * it to do, which is reach out to our repository for more information

48
   *************************************************************************

49
   */
50
51
  //ensure that the index function calls our repository's "findAll" method

52
  public function testIndexShouldCallFindAllMethod()
53
  {
54
    //create our new Mockery object with a name of CommentRepositoryInterface

55
    $mock = Mockery::mock('CommentRepositoryInterface');
56
57
    //inform the Mockery object that the "findAll" method should be called on it once

58
    //and return a string value of "foo"

59
    $mock->shouldReceive('findAll')->once()->andReturn('foo');
60
61
    //inform our application that we have an instance that it should use

62
    //whenever the CommentRepositoryInterface is requested

63
    App::instance('CommentRepositoryInterface', $mock);
64
65
    //call our controller route

66
    $response = $this->call('GET', route('v1.posts.comments.index', array(1)));
67
68
    //assert that the response is a boolean value of true

69
    $this->assertTrue(!! $response->original);
70
  }
71
72
  //ensure that the show method calls our repository's "findById" method

73
  public function testShowShouldCallFindById()
74
  {
75
    $mock = Mockery::mock('CommentRepositoryInterface');
76
    $mock->shouldReceive('findById')->once()->andReturn('foo');
77
    App::instance('CommentRepositoryInterface', $mock);
78
79
    $response = $this->call('GET', route('v1.posts.comments.show', array(1,1)));
80
    $this->assertTrue(!! $response->original);
81
  }
82
83
  //ensure that our create method calls the "instance" method on the repository

84
  public function testCreateShouldCallInstanceMethod()
85
  {
86
    $mock = Mockery::mock('CommentRepositoryInterface');
87
    $mock->shouldReceive('instance')->once()->andReturn(array());
88
    App::instance('CommentRepositoryInterface', $mock);
89
90
    $response = $this->call('GET', route('v1.posts.comments.create', array(1)));
91
    $this->assertViewHas('comment');
92
  }
93
94
  //ensure that the edit method calls our repository's "findById" method

95
  public function testEditShouldCallFindByIdMethod()
96
  {
97
    $mock = Mockery::mock('CommentRepositoryInterface');
98
    $mock->shouldReceive('findById')->once()->andReturn(array());
99
    App::instance('CommentRepositoryInterface', $mock);
100
101
    $response = $this->call('GET', route('v1.posts.comments.edit', array(1,1)));
102
    $this->assertViewHas('comment');
103
  }
104
105
  //ensure that the store method should call the repository's "store" method

106
  public function testStoreShouldCallStoreMethod()
107
  {
108
    $mock = Mockery::mock('CommentRepositoryInterface');
109
    $mock->shouldReceive('store')->once()->andReturn('foo');
110
    App::instance('CommentRepositoryInterface', $mock);
111
112
    $response = $this->call('POST', route('v1.posts.comments.store', array(1)));
113
    $this->assertTrue(!! $response->original);
114
  }
115
116
  //ensure that the update method should call the repository's "update" method

117
  public function testUpdateShouldCallUpdateMethod()
118
  {
119
    $mock = Mockery::mock('CommentRepositoryInterface');
120
    $mock->shouldReceive('update')->once()->andReturn('foo');
121
    App::instance('CommentRepositoryInterface', $mock);
122
123
    $response = $this->call('PUT', route('v1.posts.comments.update', array(1,1)));
124
    $this->assertTrue(!! $response->original);
125
  }
126
127
  //ensure that the destroy method should call the repositories "destroy" method

128
  public function testDestroyShouldCallDestroyMethod()
129
  {
130
    $mock = Mockery::mock('CommentRepositoryInterface');
131
    $mock->shouldReceive('destroy')->once()->andReturn(true);
132
    App::instance('CommentRepositoryInterface', $mock);
133
134
    $response = $this->call('DELETE', route('v1.posts.comments.destroy', array(1,1)));
135
    $this->assertTrue( empty($response->original) );
136
  }
137
138
139
}

app/tests/controllers/PostsControllerTest.php

Next, we'll follow the exact same procedure for the PostsController tests

1
<?php
2
3
class PostsControllerTest extends TestCase {
4
5
  /**

6
   * Test Basic Route Responses

7
   */
8
  public function testIndex()
9
  {
10
    $response = $this->call('GET', route('v1.posts.index'));
11
    $this->assertTrue($response->isOk());
12
  }
13
14
  public function testShow()
15
  {
16
    $response = $this->call('GET', route('v1.posts.show', array(1)));
17
    $this->assertTrue($response->isOk());
18
  }
19
20
  public function testCreate()
21
  {
22
    $response = $this->call('GET', route('v1.posts.create'));
23
    $this->assertTrue($response->isOk());
24
  }
25
26
  public function testEdit()
27
  {
28
    $response = $this->call('GET', route('v1.posts.edit', array(1)));
29
    $this->assertTrue($response->isOk());
30
  }
31
32
  /**

33
   * Test that controller calls repo as we expect

34
   */
35
  public function testIndexShouldCallFindAllMethod()
36
  {
37
    $mock = Mockery::mock('PostRepositoryInterface');
38
    $mock->shouldReceive('findAll')->once()->andReturn('foo');
39
    App::instance('PostRepositoryInterface', $mock);
40
41
    $response = $this->call('GET', route('v1.posts.index'));
42
    $this->assertTrue(!! $response->original);
43
  }
44
45
  public function testShowShouldCallFindById()
46
  {
47
    $mock = Mockery::mock('PostRepositoryInterface');
48
    $mock->shouldReceive('findById')->once()->andReturn('foo');
49
    App::instance('PostRepositoryInterface', $mock);
50
51
    $response = $this->call('GET', route('v1.posts.show', array(1)));
52
    $this->assertTrue(!! $response->original);
53
  }
54
55
  public function testCreateShouldCallInstanceMethod()
56
  {
57
    $mock = Mockery::mock('PostRepositoryInterface');
58
    $mock->shouldReceive('instance')->once()->andReturn(array());
59
    App::instance('PostRepositoryInterface', $mock);
60
61
    $response = $this->call('GET', route('v1.posts.create'));
62
    $this->assertViewHas('post');
63
  }
64
65
  public function testEditShouldCallFindByIdMethod()
66
  {
67
    $mock = Mockery::mock('PostRepositoryInterface');
68
    $mock->shouldReceive('findById')->once()->andReturn(array());
69
    App::instance('PostRepositoryInterface', $mock);
70
71
    $response = $this->call('GET', route('v1.posts.edit', array(1)));
72
    $this->assertViewHas('post');
73
  }
74
75
  public function testStoreShouldCallStoreMethod()
76
  {
77
    $mock = Mockery::mock('PostRepositoryInterface');
78
    $mock->shouldReceive('store')->once()->andReturn('foo');
79
    App::instance('PostRepositoryInterface', $mock);
80
81
    $response = $this->call('POST', route('v1.posts.store'));
82
    $this->assertTrue(!! $response->original);
83
  }
84
85
  public function testUpdateShouldCallUpdateMethod()
86
  {
87
    $mock = Mockery::mock('PostRepositoryInterface');
88
    $mock->shouldReceive('update')->once()->andReturn('foo');
89
    App::instance('PostRepositoryInterface', $mock);
90
91
    $response = $this->call('PUT', route('v1.posts.update', array(1)));
92
    $this->assertTrue(!! $response->original);
93
  }
94
95
  public function testDestroyShouldCallDestroyMethod()
96
  {
97
    $mock = Mockery::mock('PostRepositoryInterface');
98
    $mock->shouldReceive('destroy')->once()->andReturn(true);
99
    App::instance('PostRepositoryInterface', $mock);
100
101
    $response = $this->call('DELETE', route('v1.posts.destroy', array(1)));
102
    $this->assertTrue( empty($response->original) );
103
  }
104
105
}

app/tests/repositories/EloquentCommentRepositoryTest.php

Now for the repository tests. In writing our controller tests, we pretty much already decided what most of the interface should look like for the repositories. Our controllers needed the following methods:

  • findById($id)
  • findAll()
  • instance($data)
  • store($data)
  • update($id, $data)
  • destroy($id)

The only other method that we'll want to add here is a validate method. This will mainly be a private method for the repository to ensure that the data is safe to store or update.

For these tests, we're also going to add a setUp method, which will allow us to run some code on our class, prior to the execution of each test. Our setUp method will be a very simple one, we'll just make sure that any setUp methods defined in parent classes are also called using parent::setUp() and then simply add a class variable that stores an instance of our repository.

We'll use the power of Laravel's IoC container again to get an instance of our repository. The App::make() command will return an instance of the requested class, now it may seem strange that we do not just do $this->repo = new EloquentCommentRepository(), but hold that thought, we'll come back to it momentarily. You probably noticed that we're asking for a class called EloquentCommentRepository, but in our controller tests above, we were calling our repository CommentRepositoryInterface... put this thought on the back-burner as well... explainations for both are coming, I promise!

1
<?php
2
3
class EloquentCommentRepositoryTest extends TestCase {
4
5
  public function setUp()
6
  {
7
    parent::setUp();
8
    $this->repo = App::make('EloquentCommentRepository');
9
  }
10
11
  public function testFindByIdReturnsModel()
12
  {
13
    $comment = $this->repo->findById(1,1);
14
    $this->assertTrue($comment instanceof Illuminate\Database\Eloquent\Model);
15
  }
16
17
  public function testFindAllReturnsCollection()
18
  {
19
    $comments = $this->repo->findAll(1);
20
    $this->assertTrue($comments instanceof Illuminate\Database\Eloquent\Collection);
21
  }
22
23
  public function testValidatePasses()
24
  {
25
    $reply = $this->repo->validate(array(
26
      'post_id'   => 1,
27
      'content'   => 'Lorem ipsum Fugiat consectetur laborum Ut consequat aliqua.',
28
      'author_name' => 'Testy McTesterson'
29
    ));
30
31
    $this->assertTrue($reply);
32
  }
33
34
  public function testValidateFailsWithoutContent()
35
  {
36
    try {
37
      $reply = $this->repo->validate(array(
38
        'post_id'   => 1,
39
        'author_name' => 'Testy McTesterson'
40
      ));
41
    }
42
    catch(ValidationException $expected)
43
    {
44
      return;
45
    }
46
47
    $this->fail('ValidationException was not raised');
48
  }
49
50
  public function testValidateFailsWithoutAuthorName()
51
  {
52
    try {
53
      $reply = $this->repo->validate(array(
54
        'post_id'   => 1,
55
        'content'   => 'Lorem ipsum Fugiat consectetur laborum Ut consequat aliqua.'
56
      ));
57
    }
58
    catch(ValidationException $expected)
59
    {
60
      return;
61
    }
62
63
    $this->fail('ValidationException was not raised');
64
  }
65
66
  public function testValidateFailsWithoutPostId()
67
  {
68
    try {
69
      $reply = $this->repo->validate(array(
70
        'author_name' => 'Testy McTesterson',
71
        'content'   => 'Lorem ipsum Fugiat consectetur laborum Ut consequat aliqua.'
72
      ));
73
    }
74
    catch(ValidationException $expected)
75
    {
76
      return;
77
    }
78
79
    $this->fail('ValidationException was not raised');
80
  }
81
82
  public function testStoreReturnsModel()
83
  {
84
    $comment_data = array(
85
      'content'   => 'Lorem ipsum Fugiat consectetur laborum Ut consequat aliqua.',
86
      'author_name' => 'Testy McTesterson'
87
    );
88
89
    $comment = $this->repo->store(1, $comment_data);
90
91
    $this->assertTrue($comment instanceof Illuminate\Database\Eloquent\Model);
92
    $this->assertTrue($comment->content === $comment_data['content']);
93
    $this->assertTrue($comment->author_name === $comment_data['author_name']);
94
  }
95
96
  public function testUpdateSaves()
97
  {
98
    $comment_data = array(
99
      'content' => 'The Content Has Been Updated'
100
    );
101
102
    $comment = $this->repo->update(1, 1, $comment_data);
103
104
    $this->assertTrue($comment instanceof Illuminate\Database\Eloquent\Model);
105
    $this->assertTrue($comment->content === $comment_data['content']);
106
  }
107
108
  public function testDestroySaves()
109
  {
110
    $reply = $this->repo->destroy(1,1);
111
    $this->assertTrue($reply);
112
113
    try {
114
      $this->repo->findById(1,1);
115
    }
116
    catch(NotFoundException $expected)
117
    {
118
      return;
119
    }
120
121
    $this->fail('NotFoundException was not raised');
122
  }
123
124
  public function testInstanceReturnsModel()
125
  {
126
    $comment = $this->repo->instance();
127
    $this->assertTrue($comment instanceof Illuminate\Database\Eloquent\Model);
128
  }
129
130
  public function testInstanceReturnsModelWithData()
131
  {
132
    $comment_data = array(
133
      'title' => 'Un-validated title'
134
    );
135
136
    $comment = $this->repo->instance($comment_data);
137
    $this->assertTrue($comment instanceof Illuminate\Database\Eloquent\Model);
138
    $this->assertTrue($comment->title === $comment_data['title']);
139
  }
140
141
}

app/tests/repositories/EloquentPostRepositoryTest.php

1
<?php
2
3
class EloquentPostRepositoryTest extends TestCase {
4
5
  public function setUp()
6
  {
7
    parent::setUp();
8
    $this->repo = App::make('EloquentPostRepository');
9
  }
10
11
  public function testFindByIdReturnsModel()
12
  {
13
    $post = $this->repo->findById(1);
14
    $this->assertTrue($post instanceof Illuminate\Database\Eloquent\Model);
15
  }
16
17
  public function testFindAllReturnsCollection()
18
  {
19
    $posts = $this->repo->findAll();
20
    $this->assertTrue($posts instanceof Illuminate\Database\Eloquent\Collection);
21
  }
22
23
  public function testValidatePasses()
24
  {
25
    $reply = $this->repo->validate(array(
26
      'title'    => 'This Should Pass',
27
      'content'   => 'Lorem ipsum Fugiat consectetur laborum Ut consequat aliqua.',
28
      'author_name' => 'Testy McTesterson'
29
    ));
30
31
    $this->assertTrue($reply);
32
  }
33
34
  public function testValidateFailsWithoutTitle()
35
  {
36
    try {
37
      $reply = $this->repo->validate(array(
38
        'content'   => 'Lorem ipsum Fugiat consectetur laborum Ut consequat aliqua.',
39
        'author_name' => 'Testy McTesterson'
40
      ));
41
    }
42
    catch(ValidationException $expected)
43
    {
44
      return;
45
    }
46
47
    $this->fail('ValidationException was not raised');
48
  }
49
50
  public function testValidateFailsWithoutAuthorName()
51
  {
52
    try {
53
      $reply = $this->repo->validate(array(
54
        'title'    => 'This Should Pass',
55
        'content'   => 'Lorem ipsum Fugiat consectetur laborum Ut consequat aliqua.'
56
      ));
57
    }
58
    catch(ValidationException $expected)
59
    {
60
      return;
61
    }
62
63
    $this->fail('ValidationException was not raised');
64
  }
65
66
  public function testStoreReturnsModel()
67
  {
68
    $post_data = array(
69
      'title'    => 'This Should Pass',
70
      'content'   => 'Lorem ipsum Fugiat consectetur laborum Ut consequat aliqua.',
71
      'author_name' => 'Testy McTesterson'
72
    );
73
74
    $post = $this->repo->store($post_data);
75
76
    $this->assertTrue($post instanceof Illuminate\Database\Eloquent\Model);
77
    $this->assertTrue($post->title === $post_data['title']);
78
    $this->assertTrue($post->content === $post_data['content']);
79
    $this->assertTrue($post->author_name === $post_data['author_name']);
80
  }
81
82
  public function testUpdateSaves()
83
  {
84
    $post_data = array(
85
      'title' => 'The Title Has Been Updated'
86
    );
87
88
    $post = $this->repo->update(1, $post_data);
89
90
    $this->assertTrue($post instanceof Illuminate\Database\Eloquent\Model);
91
    $this->assertTrue($post->title === $post_data['title']);
92
  }
93
94
  public function testDestroySaves()
95
  {
96
    $reply = $this->repo->destroy(1);
97
    $this->assertTrue($reply);
98
99
    try {
100
      $this->repo->findById(1);
101
    }
102
    catch(NotFoundException $expected)
103
    {
104
      return;
105
    }
106
107
    $this->fail('NotFoundException was not raised');
108
  }
109
110
  public function testInstanceReturnsModel()
111
  {
112
    $post = $this->repo->instance();
113
    $this->assertTrue($post instanceof Illuminate\Database\Eloquent\Model);
114
  }
115
116
  public function testInstanceReturnsModelWithData()
117
  {
118
    $post_data = array(
119
      'title' => 'Un-validated title'
120
    );
121
122
    $post = $this->repo->instance($post_data);
123
    $this->assertTrue($post instanceof Illuminate\Database\Eloquent\Model);
124
    $this->assertTrue($post->title === $post_data['title']);
125
  }
126
127
}

Now that we have all of our tests in place, let's run PHPUnit again to watch them fail!

1
vendor/phpunit/phpunit/phpunit.php

You should have a whole ton of failures, and in fact, the test suite probably did not even finish testing before it crashed. This is OK, that means we have followed the rules of TDD and wrote failing tests before production code. Although, typically these tests would be written one at a time and you would not move on to the next test until you had code that allowed the previous test to pass. Your terminal should probably look something like mine at the moment:

Screenshot

What's actually failing is the assertViewHas method in our controller tests. It's kind of intimidating to deal with this kind of an error when we have lumped together all of our tests without any production code at all. This is why you should always write the tests one at a time, as you'll find these errors in stride, as opposed to just a huge mess of errors at once. For now, just follow my lead into the implementation of our code.


Sidebar Discussion

Before we proceed with the implementations, let's break for a quick sidebar discussion on the responsibilities of the MVC pattern.

From The Gang of Four:

The Model is the application object, the View is its screen presentation, and the Controller defines the way the user interface reacts to user input.

The point of using a structure like this is to remain encapsulated and flexible, allowing us to exchange and reuse components. Let's go through each part of the MVC pattern and talk about its reusability and flexibility:

View

I think most people would agree that a View is supposed to be a simple visual representation of data and should not contain much logic. In our case, as developers for the web, our View tends to be HTML or XML.

  • reusable: always, almost anything can create a view
  • flexible: not having any real logic in these layers makes this very flexible

Controller

If a Controller "defines the way the user interface reacts to user input", then its responsibility should be to listen to user input (GET, POST, Headers, etc), and build out the current state of the application. In my opinion, a Controller should be very light and should not contain more code than is required to accomplish the above.

  • reusable: We have to remember that our Controllers return an opinionated View, so we cannot ever call that Controller method in a practical way to use any of the logic inside it. Therefore any logic placed in Controller methods, must be specific to that Controller method, if the logic is reusable, it should be placed elsewhere.
  • flexible: In most PHP MVCs, the Controller is tied directly to the route, which does not leave us very much flexibility. Laravel fixes this issue by allowing us to declare routes that use a controller, so we can now swap out our controllers with different implementations if need be:
1
Route::get('/', array(
2
  'uses' => 'SomeController@action'
3
));

Model

The Model is the "application object" in our definition from the Gang of Four. This is a very generic definition. In addition, we just decided to offload any logic that needs to be reusable from our Controller, and since the Model is the only component left in our defined structure, it's logical to assume that this is the new home for that logic. However, I think the Model should not contain any logic like this. In my opinion, we should think of our "application object", in this case as an object that represents its place in the data-layer, whether that be a table, row, or collection entirely depends on state. The model should contain not much more than getters and setters for data (including relationships).

  • reusable: If we follow the above practice and make our Models be an object that represents its place in the database, this object remains very reusable. Any part of our system can use this model and by doing so gain complete and unopinionated access to the database.
  • flexible: Following the above practice, our Model is basically an implementation of an ORM, this allows us to be flexible, because we now have the power to change ORM's whenever we'd like to just by adding a new Model. We should probably have a pre-defined interface that our Model's must abide by, such as: all, find, create, update, delete. Implementation of a new ORM would be as simple as ensuring that the previously mentioned interface was accomodated.

Repository

Just by carefully defining our MVC components, we orphaned all kinds of logic into no-man's land. This is where Repositories come in to fill the void. Repositories become the intermediary of the Controllers and Models. A typical request would be something like this:

  • The Controller receives all user input and passes it to the repository.
  • The Repository does any "pre-gathering" actions such as validation of data, authorization, authentication, etc. If these "pre-gathering" actions are successful, then the request is passed to the Model for processing.
  • The Model will process all of the data into the data-layer, and return the current state.
  • The Repository will handle any "post-gathering" routines and return the current state to the controller.
  • The Controller will then create the appropriate view using the information provided by the repository.

Our Repository ends up as flexible and organized as we have made our Controllers and Models, allowing us to reuse this in most parts of our system, as well as being able to swap it out for another implementation if needed.

We have already seen an example of swapping out a repository for another implementation in the Controller tests above. Instead of using our default Repository, we asked the IoC container to provide the controller with an instance of a Mockery object. We have this same power for all of our components.

What we have accomplised here by adding another layer to our MVC, is a very organized, scalable, and testable system. Let's start putting the pieces in place and getting our tests to pass.


Controller Implementation

If you take a read through the controller tests, you'll see that all we really care about is how the controller is interacting with the repository. So let's see how light and simple that makes our controllers.

Note: in TDD, the objective is to do no more work than is required to make your tests pass. So we want to do the absolute bare minimum here.

app/controllers/V1/PostsController.php

1
<?php
2
namespace V1;
3
4
use BaseController; 
5
use PostRepositoryInterface; 
6
use Input;
7
use View;
8
9
class PostsController extends BaseController {
10
11
  /**

12
   * We will use Laravel's dependency injection to auto-magically

13
   * "inject" our repository instance into our controller

14
   */
15
  public function __construct(PostRepositoryInterface $posts)
16
  {
17
    $this->posts = $posts;
18
  }
19
20
  /**

21
   * Display a listing of the resource.

22
   *

23
   * @return Response

24
   */
25
  public function index()
26
  {
27
    return $this->posts->findAll();
28
  }
29
30
  /**

31
   * Show the form for creating a new resource.

32
   *

33
   * @return Response

34
   */
35
  public function create()
36
  {
37
    $post = $this->posts->instance();
38
    return View::make('posts._form', compact('post'));
39
  }
40
41
  /**

42
   * Store a newly created resource in storage.

43
   *

44
   * @return Response

45
   */
46
  public function store()
47
  {
48
    return $this->posts->store( Input::all() );
49
  }
50
51
  /**

52
   * Display the specified resource.

53
   *

54
   * @param int $id

55
   * @return Response

56
   */
57
  public function show($id)
58
  {
59
    return $this->posts->findById($id);
60
  }
61
62
  /**

63
   * Show the form for editing the specified resource.

64
   *

65
   * @param int $id

66
   * @return Response

67
   */
68
  public function edit($id)
69
  {
70
    $post = $this->posts->findById($id);
71
    return View::make('posts._form', compact('post'));
72
  }
73
74
  /**

75
   * Update the specified resource in storage.

76
   *

77
   * @param int $id

78
   * @return Response

79
   */
80
  public function update($id)
81
  {
82
    return $this->posts->update($id, Input::all());
83
  }
84
85
  /**

86
   * Remove the specified resource from storage.

87
   *

88
   * @param int $id

89
   * @return Response

90
   */
91
  public function destroy($id)
92
  {
93
    $this->posts->destroy($id);
94
    return '';
95
  }
96
97
}

app/controllers/PostsCommentsController.php

1
<?php
2
namespace V1;
3
4
use BaseController; 
5
use CommentRepositoryInterface; 
6
use Input;
7
use View;
8
9
class PostsCommentsController extends BaseController {
10
11
  /**

12
   * We will use Laravel's dependency injection to auto-magically

13
   * "inject" our repository instance into our controller

14
   */
15
  public function __construct(CommentRepositoryInterface $comments)
16
  {
17
    $this->comments = $comments;
18
  }
19
20
  /**

21
   * Display a listing of the resource.

22
   *

23
   * @return Response

24
   */
25
  public function index($post_id)
26
  {
27
    return $this->comments->findAll($post_id);
28
  }
29
30
  /**

31
   * Show the form for creating a new resource.

32
   *

33
   * @return Response

34
   */
35
  public function create($post_id)
36
  {
37
    $comment = $this->comments->instance(array(
38
      'post_id' => $post_id
39
    ));
40
41
    return View::make('comments._form', compact('comment'));
42
  }
43
44
  /**

45
   * Store a newly created resource in storage.

46
   *

47
   * @return Response

48
   */
49
  public function store($post_id)
50
  {
51
    return $this->comments->store( $post_id, Input::all() );
52
  }
53
54
  /**

55
   * Display the specified resource.

56
   *

57
   * @param int $id

58
   * @return Response

59
   */
60
  public function show($post_id, $id)
61
  {
62
    return $this->comments->findById($post_id, $id);
63
  }
64
65
  /**

66
   * Show the form for editing the specified resource.

67
   *

68
   * @param int $id

69
   * @return Response

70
   */
71
  public function edit($post_id, $id)
72
  {
73
    $comment = $this->comments->findById($post_id, $id);
74
75
    return View::make('comments._form', compact('comment'));
76
  }
77
78
  /**

79
   * Update the specified resource in storage.

80
   *

81
   * @param int $id

82
   * @return Response

83
   */
84
  public function update($post_id, $id)
85
  {
86
    return $this->comments->update($post_id, $id, Input::all());
87
  }
88
89
  /**

90
   * Remove the specified resource from storage.

91
   *

92
   * @param int $id

93
   * @return Response

94
   */
95
  public function destroy($post_id, $id)
96
  {
97
    $this->comments->destroy($post_id, $id);
98
    return '';
99
  }
100
101
}

It doesn't get much simpler than that, all the Controllers are doing is handing the input data to the repository, taking the response from that, and handing it to the View, the View in our case is merely JSON for most of our methods. When we return an Eloquent Collection, or Eloquent Model from a Controller in Laravel 4, the object is parsed into JSON auto-magically, which makes our job very easy.

Note: notice that we added a few more "use" statements to the top of the file to support the other classes that we're using. Do not forget this when you're working within a namespace.

The only thing that is a bit tricky in this controller is the constructor. Notice we're passing in a typed variable as a dependency for this Controller, yet there is no point that we have access to the instantiation of this controller to actually insert that class... welcome to dependency injection! What we're actually doing here is hinting to our controller that we have a dependency needed to run this class and what its class name is (or its IoC binding name). Laravel uses App::make() to create its Controllers before calling them. App::make() will try to resolve an item by looking for any bindings that we may have declared, and/or using the auto-loader to provide an instance. In addition, it will also resolve any dependencies needed to instantiate that class for us, by more-or-less recursively calling App::make() on each of the dependencies.

The observant, will notice that what we're trying to pass in as a dependency is an interface, and as you know, an interface cannot be instantiated. This is where it gets cool and we actually already did the same thing in our tests. In our tests however, we used App::instance() to provide an already created instance instead of the interface. For our Controllers, we're actually going to tell Laravel that whenever an instance of PostRepositoryInterface is requested, to actually return an instance of EloquentPostRepository.

Open up your app/routes.php file and add the following to the top of the file

1
App::bind('PostRepositoryInterface', 'EloquentPostRepository');
2
App::bind('CommentRepositoryInterface', 'EloquentCommentRepository');

After adding those lines, anytime App::make() asks for an instance of PostRepositoryInterface, it will create an instance of EloquentPostRepository, which is assumed to implement PostRepositoryInterface. If you were to ever change your repository to instead use a different ORM than Eloquent, or maybe a file-based driver, all you have to do is change these two lines and you're good to go, your Controllers will still work as normal. The Controllers actual dependency is any object that implements that interface and we can determine at run-time what that implementation actually is.

The PostRepositoryInterface and CommentRepositoryInterface must actually exist and the bindings must actually implement them. So let's create them now:

app/repositories/PostRepositoryInterface.php

1
<?php
2
3
interface PostRepositoryInterface {
4
  public function findById($id);
5
  public function findAll();
6
  public function paginate($limit = null);
7
  public function store($data);
8
  public function update($id, $data);
9
  public function destroy($id);
10
  public function validate($data);
11
  public function instance();
12
}

app/repositories/CommentRepositoryInterface.php

1
<?php
2
3
interface CommentRepositoryInterface {
4
  public function findById($post_id, $id);
5
  public function findAll($post_id);
6
  public function store($post_id, $data);
7
  public function update($post_id, $id, $data);
8
  public function destroy($post_id, $id);
9
  public function validate($data);
10
  public function instance();
11
}

Now that we have our two interfaces built, we must provide implementations of these interfaces. Let's build them now.

app/repositories/EloquentPostRepository.php

As the name of this implementation implies, we're relying on Eloquent, which we can call directly. If you had other dependencies, remember that App::make() is being used to resolve this repository, so you can feel free to use the same constructor method we used with our Controllers to inject your dependencies.

1
<?php
2
3
class EloquentPostRepository implements PostRepositoryInterface {
4
5
  public function findById($id)
6
  {
7
    $post = Post::with(array(
8
        'comments' => function($q)
9
        {
10
          $q->orderBy('created_at', 'desc');
11
        }
12
      ))
13
      ->where('id', $id)
14
      ->first();
15
16
    if(!$post) throw new NotFoundException('Post Not Found');
17
    return $post;
18
  }
19
20
  public function findAll()
21
  {
22
    return Post::with(array(
23
        'comments' => function($q)
24
        {
25
          $q->orderBy('created_at', 'desc');
26
        }
27
      ))
28
      ->orderBy('created_at', 'desc')
29
      ->get();
30
  }
31
32
  public function paginate($limit = null)
33
  {
34
    return Post::paginate($limit);
35
  }
36
37
  public function store($data)
38
  {
39
    $this->validate($data);
40
    return Post::create($data);
41
  }
42
43
  public function update($id, $data)
44
  {
45
    $post = $this->findById($id);
46
    $post->fill($data);
47
    $this->validate($post->toArray());
48
    $post->save();
49
    return $post;
50
  }
51
52
  public function destroy($id)
53
  {
54
    $post = $this->findById($id);
55
    $post->delete();
56
    return true;
57
  }
58
59
  public function validate($data)
60
  {
61
    $validator = Validator::make($data, Post::$rules);
62
    if($validator->fails()) throw new ValidationException($validator);
63
    return true;
64
  }
65
66
  public function instance($data = array())
67
  {
68
    return new Post($data);
69
  }
70
71
}

app/repositories/EloquentCommentRepository.php

1
<?php
2
3
class EloquentCommentRepository implements CommentRepositoryInterface {
4
5
  public function findById($post_id, $id)
6
  {
7
    $comment = Comment::find($id);
8
    if(!$comment || $comment->post_id != $post_id) throw new NotFoundException('Comment Not Found');
9
    return $comment;
10
  }
11
12
  public function findAll($post_id)
13
  {
14
    return Comment::where('post_id', $post_id)
15
      ->orderBy('created_at', 'desc')
16
      ->get();
17
  }
18
19
  public function store($post_id, $data)
20
  {
21
    $data['post_id'] = $post_id;
22
    $this->validate($data);
23
    return Comment::create($data);
24
  }
25
26
  public function update($post_id, $id, $data)
27
  {
28
    $comment = $this->findById($post_id, $id);
29
    $comment->fill($data);
30
    $this->validate($comment->toArray());
31
    $comment->save();
32
    return $comment;
33
  }
34
35
  public function destroy($post_id, $id)
36
  {
37
    $comment = $this->findById($post_id, $id);
38
    $comment->delete();
39
    return true;
40
  }
41
42
  public function validate($data)
43
  {
44
    $validator = Validator::make($data, Comment::$rules);
45
    if($validator->fails()) throw new ValidationException($validator);
46
    return true;
47
  }
48
49
  public function instance($data = array())
50
  {
51
    return new Comment($data);
52
  }
53
54
}

If you take a look in our repositories, there are a few Exceptions that we are throwing, which are not native, nor do they belong to Laravel. Those are custom Exceptions that we're using to simplify our code. By using custom Exceptions, we're able to easily halt the progress of the application if certain conditions are met. For instance, if a post is not found, we can just toss a NotFoundException, and the application will handle it accordingly, but, not by showing a 500 error as usual, instead we're going to setup custom error handlers. You could alternatively use App::abort(404) or something along those lines, but I find that this method saves me many conditional statements and repeat code, as well as allowing me to adjust the implementation of error reporting in a single place very easily.

First let's define the custom Exceptions. Create a file in your app folder called errors.php

1
touch app/errors.php

app/errors.php

1
<?php
2
3
class PermissionException extends Exception {
4
5
  public function __construct($message = null, $code = 403)
6
  {
7
    parent::__construct($message ?: 'Action not allowed', $code);
8
  }
9
10
}
11
12
class ValidationException extends Exception {
13
14
  protected $messages;
15
16
  /**

17
   * We are adjusting this constructor to receive an instance

18
   * of the validator as opposed to a string to save us some typing

19
   * @param Validator $validator failed validator object

20
   */
21
  public function __construct($validator)
22
  {
23
    $this->messages = $validator->messages();
24
    parent::__construct($this->messages, 400);
25
  }
26
27
  public function getMessages()
28
  {
29
    return $this->messages;
30
  }
31
32
}
33
34
class NotFoundException extends Exception {
35
36
  public function __construct($message = null, $code = 404)
37
  {
38
    parent::__construct($message ?: 'Resource Not Found', $code);
39
  }
40
41
}

These are very simple Exceptions, notice for the ValidationException, we can just pass it the failed validator instance and it will handle the error messages accordingly!

Now we need to define our error handlers that will be called when one of these Exceptions are thrown. These are basically Event listeners, whenever one of these exceptions are thrown, it's treated as an Event and calls the appropriate function. It's very simple to add logging or any other error handling procedures here.

app/filters.php

1
...
2
3
/**

4
 * General HttpException handler

5
 */
6
App::error( function(Symfony\Component\HttpKernel\Exception\HttpException $e, $code)
7
{
8
  $headers = $e->getHeaders();
9
10
  switch($code)
11
  {
12
    case 401:
13
      $default_message = 'Invalid API key';
14
      $headers['WWW-Authenticate'] = 'Basic realm="CRM REST API"';
15
    break;
16
17
    case 403:
18
      $default_message = 'Insufficient privileges to perform this action';
19
    break;
20
21
    case 404:
22
      $default_message = 'The requested resource was not found';
23
    break;
24
25
    default:
26
      $default_message = 'An error was encountered';
27
  }
28
29
  return Response::json(array(
30
    'error' => $e->getMessage() ?: $default_message
31
  ), $code, $headers);
32
});
33
34
/**

35
 * Permission Exception Handler

36
 */
37
App::error(function(PermissionException $e, $code)
38
{
39
  return Response::json($e->getMessage(), $e->getCode());
40
});
41
42
/**

43
 * Validation Exception Handler

44
 */
45
App::error(function(ValidationException $e, $code)
46
{
47
  return Response::json($e->getMessages(), $code);
48
});
49
50
/**

51
 * Not Found Exception Handler

52
 */
53
App::error(function(NotFoundException $e)
54
{
55
  return Response::json($e->getMessage(), $e->getCode());
56
});

We now need to let our auto-loader know about these new files. So we must tell Composer where to check for them:

composer.json

Notice that we added the "app/errors.php" line.

1
{
2
  "require": {
3
    "laravel/framework": "4.0.*",
4
    "way/generators": "dev-master",
5
    "twitter/bootstrap": "dev-master",
6
    "conarwelsh/mustache-l4": "dev-master"
7
  },
8
  "require-dev": {
9
    "phpunit/phpunit": "3.7.*",
10
    "mockery/mockery": "0.7.*"
11
  },
12
  "autoload": {
13
    "classmap": [
14
      "app/commands",
15
      "app/controllers",
16
      "app/models",
17
      "app/database/migrations",
18
      "app/database/seeds",
19
      "app/tests/TestCase.php",
20
      "app/repositories",
21
      "app/errors.php"
22
    ]
23
  },
24
  "scripts": {
25
    "post-update-cmd": "php artisan optimize"
26
  },
27
  "minimum-stability": "dev"
28
}

We must now tell Composer to actually check for these files and include them in the auto-load registry.

1
composer dump-autoload

Great, so we have completed our controllers and our repositories, the last two items in our MVRC that we have to take care of is the models and views, both of which are pretty straight forward.

app/models/Post.php

1
<?php
2
/**

3
 * Represent a Post Item, or Collection

4
 */
5
class Post extends Eloquent {
6
7
  /**

8
   * Items that are "fillable"

9
   * meaning we can mass-assign them from the constructor

10
   * or $post->fill()

11
   * @var array

12
   */
13
  protected $fillable = array(
14
    'title', 'content', 'author_name'
15
  );
16
17
  /**

18
   * Validation Rules

19
   * this is just a place for us to store these, you could

20
   * alternatively place them in your repository

21
   * @var array

22
   */
23
  public static $rules = array(
24
    'title'    => 'required',
25
    'author_name' => 'required'
26
  );
27
28
  /**

29
   * Define the relationship with the comments table

30
   * @return Collection collection of Comment Models

31
   */
32
  public function comments()
33
  {
34
    return $this->hasMany('Comment');
35
  }
36
37
}

app/models/Comment.php

1
<?php
2
/**

3
 * Represent a Comment Item, or Collection

4
 */
5
class Comment extends Eloquent {
6
7
  /**

8
   * Items that are "fillable"

9
   * meaning we can mass-assign them from the constructor

10
   * or $comment->fill()

11
   * @var array

12
   */
13
  protected $fillable = array(
14
    'post_id', 'content', 'author_name'
15
  );
16
17
  /**

18
   * Validation Rules

19
   * this is just a place for us to store these, you could

20
   * alternatively place them in your repository

21
   * @var array

22
   */
23
  public static $rules = array(
24
    'post_id'   => 'required|numeric',
25
    'content'   => 'required',
26
    'author_name' => 'required'
27
  );
28
29
  /**

30
   * Define the relationship with the posts table

31
   * @return Model parent Post model

32
   */
33
  public function post()
34
  {
35
    return $this->belongsTo('Post');
36
  }
37
38
}

As far as views are concerned, I'm just going to mark up some simple bootstrap-friendly pages. Remember to change each files extension to .mustache though, since our generator thought that we would be using .blade.php. We're also going to create a few "partial" views using the Rails convention of prefixing them with an _ to signify a partial.

Note: I skipped a few views, as we will not be using them in this tutorial.

public/views/posts/index.mustache

For the index page view we'll just loop over all of our posts, showing the post partial for each.

1
{{#posts}}
2
  {{> posts._post}}
3
{{/posts}}

public/views/posts/show.mustache

For the show view we'll show an entire post and its comments:

1
<article>
2
  <h3>
3
    {{ post.title }} {{ post.id }}
4
    <small>{{ post.author_name }}</small>
5
  </h3>
6
  <div>
7
    {{ post.content }}
8
  </div>
9
</article>
10
11
<div>
12
  <h2>Add A Comment</h2>
13
  {{> comments._form }}
14
15
  <section data-role="comments">
16
    {{#post.comments}}
17
      <div>
18
        {{> comments._comment }}
19
      </div>
20
    {{/post.comments}}
21
  </section>
22
</div>

public/views/posts/_post.mustache

Here's the partial that we'll use to show a post in a list. This is used on our index view.

1
<article data-toggle="view" data-target="posts/{{ id }}">
2
  <h3>{{ title }} {{ id }}</h3>
3
  <cite>{{ author_name }} on {{ created_at }}</cite>
4
</article>

public/views/posts/_form.mustache

Here's the form partial needed to create a post, we'll use this from our API, but this could also be a useful view in an admin panel and other places, which is why we choose to make it a partial.

1
{{#exists}}
2
  <form action="/v1/posts/{{ post.id }}" method="post">
3
    <input type="hidden" name="_method" value="PUT" />
4
{{/exists}}
5
{{^exists}}
6
  <form action="/v1/posts" method="post">
7
{{/exists}}
8
9
  <fieldset>
10
11
    <div class="control-group">
12
      <label class="control-label"></label>
13
      <div class="controls">
14
        <input type="text" name="title" value="{{ post.title }}" />
15
      </div>
16
    </div>
17
18
    <div class="control-group">
19
      <label class="control-label"></label>
20
      <div class="controls">
21
        <input type="text" name="author_name" value="{{ post.author_name }}" />
22
      </div>
23
    </div>
24
25
    <div class="control-group">
26
      <label class="control-label"></label>
27
      <div class="controls">
28
        <textarea name="content">{{ post.content }}"</textarea>
29
      </div>
30
    </div>
31
32
    <div class="form-actions">
33
      <input type="submit" class="btn btn-primary" value="Save" />
34
    </div>
35
36
  </fieldset>
37
</form>

public/views/comments/_comment.mustache

Here's the comment partial which is used to represent a single comment in a list of comments:

1
<h5>
2
  {{ author_name }}
3
  <small>{{ created_at }}</small>
4
</h5>
5
<div>
6
  {{ content }}
7
</div>

public/views/comments/_form.mustache

The form needed to create a comment - both used in the API and the Show Post view:

1
{{#exists}}
2
  <form class="form-horizontal" action="/v1/posts/{{ comment.post_id }}/{{ id }}" method="post">
3
    <input type="hidden" name="_method" value="PUT" />
4
{{/exists}}
5
{{^exists}}
6
  <form class="form-horizontal" action="/v1/posts/{{ comment.post_id }}" method="post">
7
{{/exists}}
8
9
  <fieldset>
10
11
    <div class="control-group">
12
      <label class="control-label">Author Name</label>
13
      <div class="controls">
14
        <input type="text" name="author_name" value="{{ comment.author_name }}" />
15
      </div>
16
    </div>
17
18
    <div class="control-group">
19
      <label class="control-label">Comment</label>
20
      <div class="controls">
21
        <textarea name="content">{{ comment.content }}</textarea>
22
      </div>
23
    </div>
24
25
    <div class="form-actions">
26
      <input type="submit" class="btn btn-primary" value="Save" />
27
    </div>
28
29
  </fieldset>
30
</form>

public/views/layouts/_notification.mustache

And here's the helper view partial to allow us to show a notification:

1
<div class="alert alert-{{type}}">
2
  {{message}}
3
</div>

Great, we have all of our API components in place. Let's run our unit tests to see where we're at!

1
vendor/phpunit/phpunit/phpunit.php

Your first run of this test should pass with flying (green) colors. However, if you were to run this test again, you'll notice that it fails now with a handful of errors, and that is because our repository tests actually tested the database, and in doing so deleted some of the records our previous tests used to assert values. This is an easy fix, all we have to do is tell our tests that they need to re-seed the database after each test. In addition, we did not receive a noticable error for this, but we did not close Mockery after each test either, this is a requirement of Mockery that you can find in their docs. So let's add both missing methods.

Open up app/tests/TestCase.php and add the following two methods:

1
/**

2
 * setUp is called prior to each test

3
 */
4
public function setUp()
5
{
6
  parent::setUp();
7
  $this->seed();
8
}
9
10
/**

11
 * tearDown is called after each test

12
 * @return [type] [description]

13
 */
14
public function tearDown()
15
{
16
  Mockery::close();
17
}

This is great, we now said that at every "setUp", which is run before each test, to re-seed the database. However we still have one problem, everytime you re-seed, it's only going to append new rows to the tables. Our tests are looking for items with a row ID of one, so we still have a few changes to make. We just need to tell the database to truncate our tables when seeding:

app/database/seeds/CommentsTableSeeder.php

Before we insert the new rows, we'll truncate the table, deleting all rows and resetting the auto-increment counter.

1
<?php
2
3
class CommentsTableSeeder extends Seeder {
4
5
  public function run()
6
  {
7
    $comments = array(
8
      array(
9
        'content'   => 'Lorem ipsum Nisi dolore ut incididunt mollit tempor proident eu velit cillum dolore sed',
10
        'author_name' => 'Testy McTesterson',
11
        'post_id'   => 1,
12
        'created_at' => date('Y-m-d H:i:s'),
13
        'updated_at' => date('Y-m-d H:i:s'),
14
      ),
15
      array(
16
        'content'   => 'Lorem ipsum Nisi dolore ut incididunt mollit tempor proident eu velit cillum dolore sed',
17
        'author_name' => 'Testy McTesterson',
18
        'post_id'   => 1,
19
        'created_at' => date('Y-m-d H:i:s'),
20
        'updated_at' => date('Y-m-d H:i:s'),
21
      ),
22
      array(
23
        'content'   => 'Lorem ipsum Nisi dolore ut incididunt mollit tempor proident eu velit cillum dolore sed',
24
        'author_name' => 'Testy McTesterson',
25
        'post_id'   => 2,
26
        'created_at' => date('Y-m-d H:i:s'),
27
        'updated_at' => date('Y-m-d H:i:s'),
28
      ),
29
    );
30
31
    //truncate the comments table when we seed

32
    DB::table('comments')->truncate();
33
    DB::table('comments')->insert($comments);
34
  }
35
36
}

app/database/seeds/PostsTableSeeder.php

1
<?php
2
3
class PostsTableSeeder extends Seeder {
4
5
  public function run()
6
  {
7
    $posts = array(
8
      array(
9
        'title'    => 'Test Post',
10
        'content'   => 'Lorem ipsum Reprehenderit velit est irure in enim in magna aute occaecat qui velit ad.',
11
        'author_name' => 'Conar Welsh',
12
        'created_at' => date('Y-m-d H:i:s'),
13
        'updated_at' => date('Y-m-d H:i:s'),
14
      ),
15
      array(
16
        'title'    => 'Another Test Post',
17
        'content'   => 'Lorem ipsum Reprehenderit velit est irure in enim in magna aute occaecat qui velit ad.',
18
        'author_name' => 'Conar Welsh',
19
        'created_at' => date('Y-m-d H:i:s'),
20
        'updated_at' => date('Y-m-d H:i:s'),
21
      )
22
    );
23
24
    //truncate the posts table each time we seed

25
    DB::table('posts')->truncate();
26
    DB::table('posts')->insert($posts);
27
  }
28
29
}

Now you should be able to run the tests any number of times and get passing tests each time! That means we have fulfilled our TDD cycle and we're not allowed to write anymore production code for our API!! Let's just commit our changes to our repo and move onto the Backbone application!

1
git add . && git commit -am "built out the API and corresponding tests"

Backbone App

Now that we have completed all of the back-end work, we can move forward to creating a nice user interface to access all of that data. We'll keep this part of the project a little bit on the simpler side, and I warn you that my approach can be considered an opinionated one. I have seen many people with so many different methods for structuring a Backbone application. My trials and errors have led me to my current method, if you do not agree with it, my hope is that it may inspire you to find your own!

We're going to use the Mustache templating engine instead of Underscore, this will allow us to share our views between the client and server! The trick is in how you load the views, we're going to use AJAX in this tutorial, but it's just as easy to load them all into the main template, or precompile them.

Router

First we'll get our router going. There are two parts to this, the Laravel router, and the Backbone router.

Laravel Router

There are two main approaches we can take here:

Approach #1: The catch-all

Remember I told you when you were adding the resource routes that it was important that you placed them ABOVE the app route?? The catch-all method is the reason for that statement. The overall goal of this method is to have any routes that have not found a match in Laravel, be caught and sent to Backbone. Implementing this method is easy:

app/routes.php

1
// change your existing app route to this:

2
// we are basically just giving it an optional parameter of "anything"

3
Route::get('/{path?}', function($path = null)
4
{
5
  return View::make('app');
6
})
7
->where('path', '.*'); //regex to match anything (dots, slashes, letters, numbers, etc)

Now, every route other than our API routes will render our app view.

In addition, if you have a multi-page app (several single page apps), you can define several of these catch-alls:

1
Route::get('someApp1{path?}', function($path = null)
2
{
3
  return View::make('app');
4
})
5
->where('path', '.*');
6
7
Route::get('anotherApp/{path?}', function($path = null)
8
{
9
  return View::make('app');
10
})
11
->where('path', '.*');
12
13
Route::get('athirdapp{path?}', function($path = null)
14
{
15
  return View::make('app');
16
})
17
->where('path', '.*');

Note: Keep in mind the '/' before {path?}. If that slash is there, it'll be required in the URL (with the exception of the index route), sometimes this is desired and sometimes not.

Approach #2:

Since our front and back end share views... wouldn't it be extremely easy to just define routes in both places? You can even do this in addition to the catch-all approach if you want.

The routes that we're going to end up defining for the app are simply:

1
GET /
2
GET /posts/:id

app/routes.php

1
<?php
2
3
App::bind('PostRepositoryInterface', 'EloquentPostRepository'); 
4
App::bind('CommentRepositoryInterface', 'EloquentCommentRepository'); 
5
6
7
8
9
10
//create a group of routes that will belong to APIv1

11
Route::group(array('prefix' => 'v1'), function()
12
{
13
  Route::resource('posts', 'V1\PostsController');
14
  Route::resource('posts.comments', 'V1\PostsCommentsController');
15
});
16
17
18
19
/**

20
 * Method #1: use catch-all

21
 * optionally commented out while we use Method 2

22
 */
23
// change your existing app route to this:

24
// we are basically just giving it an optional parameter of "anything"

25
// Route::get('/{path?}', function($path = null)

26
// {

27
//   return View::make('layouts.application')->nest('content', 'app');

28
// })

29
// ->where('path', '.*'); //regex to match anything (dots, slashes, letters, numbers, etc)

30
31
32
33
/**

34
 * Method #2: define each route

35
 */
36
Route::get('/', function()
37
{
38
  $posts = App::make('PostRepositoryInterface')->paginate();
39
  return View::make('layouts.application')->nest('content', 'posts.index', array(
40
    'posts' => $posts
41
  ));
42
});
43
44
Route::get('posts/{id}', function($id)
45
{
46
  $post = App::make('PostRepositoryInterface')->findById($id);
47
  return View::make('layouts.application')->nest('content', 'posts.show', array(
48
    'post' => $post
49
  ));
50
});

Pretty cool huh?! Regardless of which method we use, or the combination of both, your Backbone router will end up mostly the same.

Notice that we're using our Repository again, this is yet another reason why Repositories are a useful addition to our framework. We can now run almost all of the logic that the controller does, but without repeating hardly any of the code!

Keep in mind a few things while choosing which method to use, if you use the catch-all, it will do just like the name implies... catch-ALL. This means there is no such thing as a 404 on your site anymore. No matter the request, its landing on the app page (unless you manually toss an exception somewhere such as your repository). The inverse is, with defining each route, now you have two sets of routes to manage. Both methods have their ups and downs, but both are equally easy to deal with.

Base View

One view to rule them all! This BaseView is the view that all of our other Views will inherit from. For our purposes, this view has but one job... templating! In a larger app this view is a good place to put other shared logic.

We'll simply extend Backbone.View and add a template function that will return our view from the cache if it exists, or get it via AJAX and place it in the cache. We have to use synchronous AJAX due to the way that Mustache.js fetches partials, but since we're only retrieving these views if they are not cached, we shouldn't receive much of a performance hit here.

1
/**

2
 ***************************************

3
 * Array Storage Driver

4
 * used to store our views

5
 ***************************************

6
 */
7
var ArrayStorage = function(){
8
  this.storage = {};
9
};
10
ArrayStorage.prototype.get = function(key)
11
{
12
  return this.storage[key];
13
};
14
ArrayStorage.prototype.set = function(key, val)
15
{
16
  return this.storage[key] = val;
17
};
18
19
20
21
/**

22
 ***************************************

23
 * Base View

24
 ***************************************

25
 */
26
var BaseView = bb.View.extend({
27
28
  /**

29
   * Set our storage driver

30
   */
31
  templateDriver: new ArrayStorage,
32
33
  /**

34
   * Set the base path for where our views are located

35
   */
36
  viewPath: '/views/',
37
38
  /**

39
   * Get the template, and apply the variables

40
   */
41
  template: function()
42
  {
43
    var view, data, template, self;
44
45
    switch(arguments.length)
46
    {
47
      case 1:
48
        view = this.view;
49
        data = arguments[0];
50
        break;
51
      case 2:
52
        view = arguments[0];
53
        data = arguments[1];
54
        break;
55
    }
56
57
    template = this.getTemplate(view, false);
58
    self = this;
59
60
    return template(data, function(partial)
61
    {
62
      return self.getTemplate(partial, true);
63
    });
64
  },
65
66
  /**

67
   * Facade that will help us abstract our storage engine,

68
   * should we ever want to swap to something like LocalStorage

69
   */
70
  getTemplate: function(view, isPartial)
71
  {
72
    return this.templateDriver.get(view) || this.fetch(view, isPartial);
73
  },
74
75
  /**

76
   * Facade that will help us abstract our storage engine,

77
   * should we ever want to swap to something like LocalStorage

78
   */
79
  setTemplate: function(name, template)
80
  {
81
    return this.templateDriver.set(name, template);
82
  },
83
84
  /**

85
   * Function to retrieve the template via ajax

86
   */
87
  fetch: function(view, isPartial)
88
  {
89
    var markup = $.ajax({
90
      async: false,
91
92
      //the URL of our template, we can optionally use dot notation

93
      url: this.viewPath + view.split('.').join('/') + '.mustache'
94
    }).responseText;
95
96
    return isPartial
97
      ? markup
98
      : this.setTemplate(view, Mustache.compile(markup));
99
  }
100
});

PostView

The PostView renders a single blog post:

1
// this view will show an entire post

2
// comment form, and comments

3
var PostView = BaseView.extend({
4
5
  //the location of the template this view will use, we can use dot notation

6
  view: 'posts.show',
7
8
  //events this view should subscribe to

9
  events: {
10
    'submit form': function(e)
11
    {
12
      e.preventDefault();
13
      e.stopPropagation();
14
15
      return this.addComment( $(e.target).serialize() );
16
    }
17
  },
18
19
  //render our view into the defined `el`

20
  render: function()
21
  {
22
    var self = this;
23
24
    self.$el.html( this.template({
25
      post: this.model.attributes
26
    }) );
27
  },
28
29
  //add a comment for this post

30
  addComment: function(formData)
31
  {
32
    var
33
      self = this,
34
35
      //build our url

36
      action = this.model.url() + '/comments'
37
    ;
38
39
    //submit a post to our api

40
    $.post(action, formData, function(comment, status, xhr)
41
    {
42
      //create a new comment partial

43
      var view = new CommentViewPartial({
44
        //we are using a blank backbone model, since we done have any specific logic needed

45
        model: new bb.Model(comment)
46
      });
47
48
      //prepend the comment partial to the comments list

49
      view.render().$el.prependTo(self.$('[data-role="comments"]'));
50
51
      //reset the form

52
      self.$('input[type="text"], textarea').val('');
53
54
      //prepend our new comment to the collection

55
      self.model.attributes.comments.unshift(comment);
56
57
      //send a notification that we successfully added the comment

58
      notifications.add({
59
        type: 'success',
60
        message: 'Comment Added!'
61
      });
62
    });
63
64
  }
65
});

Partial Views

We'll need a few views to render partials. We mainly just need to tell the view which template to use and that it should extend our view that provides the method to fetch our template.

1
// this will be used for rendering a single comment in a list

2
var CommentViewPartial = BaseView.extend({
3
  //define our template location

4
  view: 'comments._comment',
5
  render: function()
6
  {
7
    this.$el.html( this.template(this.model.attributes) );
8
    return this;
9
  }
10
});
11
12
//this view will be used for rendering a single post in a list

13
var PostViewPartial = BaseView.extend({
14
  //define our template location

15
  view: 'posts._post',
16
  render: function()
17
  {
18
    this.$el.html( this.template(this.model.attributes) );
19
    return this;
20
  }
21
});

Blog View

This is our overall application view. It contains our configuration logic, as well as handling the fetching of our PostCollection. We also setup a cool little infinite scroll feature. Notice how we're using jQuery promises to ensure that the fetching of our collection has completed prior to rendering the view.

1
var Blog = BaseView.extend({
2
  //define our template location

3
  view: 'posts.index',
4
5
  //setup our app configuration

6
  initialize: function()
7
  {
8
    this.perPage = this.options.perPage || 15;
9
    this.page   = this.options.page || 0;
10
    this.fetching = this.collection.fetch();
11
12
    if(this.options.infiniteScroll) this.enableInfiniteScroll();
13
  },
14
15
  //wait til the collection has been fetched, and render the view

16
  render: function()
17
  {
18
    var self = this;
19
    this.fetching.done(function()
20
    {
21
      self.$el.html('');
22
      self.addPosts();
23
24
      // var posts = this.paginate()

25
26
      // for(var i=0; i<posts.length; i++)

27
      // {

28
      //   posts[i] = posts[i].toJSON();

29
      // }

30
31
      // self.$el.html( self.template({

32
      //   posts: posts

33
      // }) );

34
35
      if(self.options.infiniteScroll) self.enableInfiniteScroll();
36
    });
37
  },
38
39
  //helper function to limit the amount of posts we show at a time

40
  paginate: function()
41
  {
42
    var posts;
43
    posts = this.collection.rest(this.perPage * this.page);
44
    posts = _.first(posts, this.perPage);
45
    this.page++;
46
47
    return posts;
48
  },
49
50
  //add the next set of posts to the view

51
  addPosts: function()
52
  {
53
    var posts = this.paginate();
54
55
    for(var i=0; i<posts.length; i++)
56
    {
57
      this.addOnePost( posts[i] );
58
    }
59
  },
60
61
  //helper function to add a single post to the view

62
  addOnePost: function(model)
63
  {
64
    var view = new PostViewPartial({
65
      model: model
66
    });
67
    this.$el.append( view.render().el );
68
  },
69
70
  //this function will show an entire post, we could alternatively make this its own View

71
  //however I personally like having it available in the overall application view, as it

72
  //makes it easier to manage the state

73
  showPost: function(id)
74
  {
75
    var self = this;
76
77
    this.disableInifiniteScroll();
78
79
    this.fetching.done(function()
80
    {
81
      var model = self.collection.get(id);
82
83
      if(!self.postView)
84
      {
85
        self.postView = new self.options.postView({
86
          el: self.el
87
        });
88
      }
89
      self.postView.model = model;
90
      self.postView.render();
91
    });
92
  },
93
94
  //function to run during the onScroll event

95
  infiniteScroll: function()
96
  {
97
    if($window.scrollTop() >= $document.height() - $window.height() - 50)
98
    {
99
      this.addPosts();
100
    }
101
  },
102
103
  //listen for the onScoll event

104
  enableInfiniteScroll: function()
105
  {
106
    var self = this;
107
108
    $window.on('scroll', function()
109
    {
110
      self.infiniteScroll();
111
    });
112
  },
113
114
  //stop listening to the onScroll event

115
  disableInifiniteScroll: function()
116
  {
117
    $window.off('scroll');
118
  }
119
});

PostCollection

Setup our PostCollection - we just need to tell the Collection the URL it should use to fetch its contents.

1
// the posts collection is configured to fetch

2
// from our API, as well as use our PostModel

3
var PostCollection = bb.Collection.extend({
4
  url: '/v1/posts'
5
});

Blog Router

Notice that we're not instantiating new instances of our views, we're merely telling them to render. Our initialize functions are designed to only be ran once, as we don't want them to run but once, on page load.

1
var BlogRouter = bb.Router.extend({
2
  routes: {
3
    "": "index",
4
    "posts/:id": "show"
5
  },
6
  initialize: function(options)
7
  {
8
    // i do this to avoid having to hardcode an instance of a view

9
    // when we instantiate the router we will pass in the view instance

10
    this.blog = options.blog;
11
  },
12
  index: function()
13
  {
14
    //reset the paginator

15
    this.blog.page = 0;
16
17
    //render the post list

18
    this.blog.render();
19
  },
20
  show: function(id)
21
  {
22
    //render the full-post view

23
    this.blog.showPost(id);
24
  }
25
});

Notifications Collection

We're just going to setup a simple Collection to store user notifications:

1
var notifications = new bb.Collection();

NotificationsView

This view will handle the displaying and hiding of user notifications:

1
var NotificationView = BaseView.extend({
2
  el: $('#notifications'),
3
  view: 'layouts._notification',
4
  initialize: function()
5
  {
6
    this.listenTo(notifications, 'add', this.render);
7
  },
8
  render: function(notification)
9
  {
10
    var $message = $( this.template(notification.toJSON()) );
11
    this.$el.append($message);
12
    this.delayedHide($message);
13
  },
14
  delayedHide: function($message)
15
  {
16
    var timeout = setTimeout(function()
17
    {
18
      $message.fadeOut(function()
19
      {
20
        $message.remove();
21
      });
22
    }, 5*1000);
23
24
    var self = this;
25
    $message.hover(
26
      function()
27
      {
28
        timeout = clearTimeout(timeout);
29
      },
30
      function()
31
      {
32
        self.delayedHide($message);
33
      }
34
    );
35
  }
36
});
37
var notificationView = new NotificationView();

Error Handling

Since we used the custom exception handlers for our API, it makes it very easy to handle any error our API may throw. Very similar to the way we defined our event listeners for our API in the app/filters.php file, we'll define event listeners for our app here. Each code that could be thrown can just show a notification very easily!

1
$.ajaxSetup({
2
  statusCode: {
3
    401: function()
4
    {
5
      notification.add({
6
        type: null, //error, success, info, null

7
        message: 'You do not have permission to do that'
8
      });
9
    },
10
    403: function()
11
    {
12
      notification.add({
13
        type: null, //error, success, info, null

14
        message: 'You do not have permission to do that'
15
      });
16
    },
17
    404: function()
18
    {
19
      notification.add({
20
        type: 'error', //error, success, info, null

21
        message: '404: Page Not Found'
22
      });
23
    },
24
    500: function()
25
    {
26
      notification.add({
27
        type: 'error', //error, success, info, null

28
        message: 'The server encountered an error'
29
      });
30
    }
31
  }
32
});

Event Listeners

We'll need a few global event listeners to help us navigate through our app without refreshing the page. We mainly just hijack the default behavior and call Backbone.history.navigate(). Notice how on our first listener, we're specifying the selector to only match those that don't have a data attribute of bypass. This will allow us to create links such as <a href="/some/non-ajax/page" data-bypass="true">link</a> that will force the page to refresh. We could also go a step further here and check whether the link is a local one, as opposed to a link to another site.

1
$document.on("click", "a[href]:not([data-bypass])", function(e){
2
  e.preventDefault();
3
  e.stopPropagation();
4
5
  var href = $(this).attr("href");
6
  bb.history.navigate(href, true);
7
});
8
9
$document.on("click", "[data-toggle='view']", function(e)
10
{
11
  e.preventDefault();
12
  e.stopPropagation();
13
14
  var
15
    self = $(this),
16
    href = self.attr('data-target') || self.attr('href')
17
  ;
18
19
  bb.history.navigate(href, true);
20
});

Start The App

Now we just need to boot the app, passing in any config values that we need. Notice the line that checks for the silentRouter global variable, this is kind of a hacky way to be able to use both back-end routing methods at the same time. This allows us to define a variable in the view called silentRouter and set it to true, meaning that the router should not actually engage the backbone route, allowing our back-end to handle the initial rendering of the page, and just wait for any needed updates or AJAX.

1
var BlogApp = new Blog({
2
  el       : $('[data-role="main"]'),
3
  collection   : new PostCollection(),
4
  postView    : PostView,
5
  perPage    : 15,
6
  page      : 0,
7
  infiniteScroll : true
8
});
9
10
var router = new BlogRouter({
11
  blog: BlogApp
12
});
13
14
if (typeof window.silentRouter === 'undefined') window.silentRouter = true;
15
16
bb.history.start({ pushState: true, root: '/', silent: window.silentRouter });

Conclusion

Notice that for the Backbone portion of our app, all we had to do was write some Javascript that knew how to interact with the pre-existing portions of our application? That's what I love about this method! It may seem like we had a lot of steps to take to get to that portion of things, but really, most of that work was just a foundation build-up. Once we got that initial foundation in place, the actual application logic falls together very simply.

Try adding another feature to this blog, such as User listings and info. The basic steps you would take would be something like this:

  • Use the generator tool to create a new "User" resource.
  • Make the necessary modifications to ensure that the UserController is in the V1 API group.
  • Create your Repository and setup the proper IoC bindings in app/routes.php.
  • Write your Controller tests one at a time using Mockery for the repository, following each test up with the proper implementation to make sure that test passes.
  • Write your Repository tests one at a time, again, following each test up with the implementation.
  • Add in the new functionality to your Backbone App. I suggest trying two different approaches to the location of the User views. Decide for yourself which is the better implementation.
    • First place them in their own routes and Main view.
    • Then try incorporating them into the overall BlogView.

I hope this gave you some insight into creating a scalable single page app and API using Laravel 4 and Backbone.js. If you have any questions, please ask them in the comment section below!

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.