Building a Phonegap App with Laravel and Angular - Part 4
Published by Matthew Daly at 13th November 2016 4:15 pm
In this instalment we'll return to the back end. What we've done so far is typical of the kind of proof of concept we might do for a client early on, before going back and implementing the full set of features later on. Now we'll go back and start to improve on that rather quick-and-dirty API by making sure we follow a few best practices.
For those of you who want to follow the Laravel Phonegap tutorials, I've created a dedicated category here for those tutorials. This category include RSS and Atom feeds, so if you only want to read those posts, you can do so. I've also done the same for the Django tutorials.
The Repository pattern
One of the issues we currently have with our API is that we're passing our Eloquent models into our controllers. This may not seem like a huge issue, but it means that our controllers are tightly coupled to the Eloquent ORM, so if we wanted to switch to another ORM, or to a completely different database such as MongoDB, we'd have to amend our controllers. That's not good.
However, using the Repository pattern we can first of all define an interface for our repository, and then create a repository class that implements that interface. That way we can interact with the repository class in our controllers, rather than using Eloquent models directly. Then, if we want to switch databases, we merely amend the repository class to change the implementation of those methods, without having to touch our controllers. Also, it makes it much easier to test our controllers in isolation, because we can easily mock our repository class using Mockery and hard-code the response, so our tests won't touch the database and will therefore run more quickly. We won't touch on that this time, but it's a very significant advantage.
If you haven't used interfaces before in PHP, they aren't that hard. They merely specify what methods an object implementing that method must have and what arguments they must accept, but do not specify the details of the implementation. This makes it easy to determine if a class implements an interface correctly, because it will throw an exception if it doesn't.
1<?php23namespace AnimalFriend\Repositories\Interfaces;45interface PetRepositoryInterface {6 public function all();78 public function findOrFail($id);910 public function create($input);11}
That's all there is to it. We define it using the interface
keyword and we specify the methods it must implement. Save this file at app/Repositories/Interfaces/PetRepositoryInterface.php
.
Next, we implement the repository class:
1<?php23namespace AnimalFriend\Repositories;45use AnimalFriend\Pet;6use AnimalFriend\Repositories\Interfaces\PetRepositoryInterface;78class EloquentPetRepository implements PetRepositoryInterface {910 private $pet;1112 public function __construct(Pet $pet)13 {14 $this->pet = $pet;15 }1617 public function all()18 {19 return $this->pet->all();20 }2122 public function findOrFail($id)23 {24 return $this->pet->findOrFail($id);25 }2627 public function create($input)28 {29 return $this->pet->create($input);30 }31}
Save this to app/Repositories/EloquentPetRepository.php
. Note how the methods closely mirror the underlying Eloquent methods, but they don't need to - you could change the underlying implementation of each method, but the repository would still work in exactly the same way.
To make this work, we need to make a few changes elsewhere. In composer.json
, we need to add the new Repositories
folder to our classmap:
1 "autoload": {2 "classmap": [3 "database",4 "app/Repositories"5 ],6 "psr-4": {7 "AnimalFriend\\": "app/"8 }9 },
And in app/Providers/AppServiceProvider.php
, we need to bind our new files:
1<?php23namespace AnimalFriend\Providers;45use Illuminate\Support\ServiceProvider;67class AppServiceProvider extends ServiceProvider8{9 /**10 * Bootstrap any application services.11 *12 * @return void13 */14 public function boot()15 {16 //17 }1819 /**20 * Register any application services.21 *22 * @return void23 */24 public function register()25 {26 $this->app->bind(27 'AnimalFriend\Repositories\Interfaces\PetRepositoryInterface',28 'AnimalFriend\Repositories\EloquentPetRepository'29 );30 }31}
With that done, we can now update app/Http/Controllers/PetController.php
to use the repository:
1<?php23namespace AnimalFriend\Http\Controllers;45use Illuminate\Http\Request;67use AnimalFriend\Http\Requests;8use AnimalFriend\Repositories\Interfaces\PetRepositoryInterface as Pet;910class PetController extends Controller11{12 private $pet;1314 public function __construct(Pet $pet) {15 $this->pet = $pet;16 }1718 /**19 * Display a listing of the resource.20 *21 * @return \Illuminate\Http\Response22 */23 public function index()24 {25 // Get all pets26 $pets = $this->pet->all();2728 // Send response29 return response()->json($pets, 200);30 }3132 /**33 * Show the form for creating a new resource.34 *35 * @return \Illuminate\Http\Response36 */37 public function create()38 {39 //40 }4142 /**43 * Store a newly created resource in storage.44 *45 * @param \Illuminate\Http\Request $request46 * @return \Illuminate\Http\Response47 */48 public function store(Request $request)49 {50 //51 }5253 /**54 * Display the specified resource.55 *56 * @param int $id57 * @return \Illuminate\Http\Response58 */59 public function show($id)60 {61 // Get pet62 $pet = $this->pet->findOrFail($id);6364 // Send response65 return response()->json($pet, 200);66 }6768 /**69 * Show the form for editing the specified resource.70 *71 * @param int $id72 * @return \Illuminate\Http\Response73 */74 public function edit($id)75 {76 //77 }7879 /**80 * Update the specified resource in storage.81 *82 * @param \Illuminate\Http\Request $request83 * @param int $id84 * @return \Illuminate\Http\Response85 */86 public function update(Request $request, $id)87 {88 //89 }9091 /**92 * Remove the specified resource from storage.93 *94 * @param int $id95 * @return \Illuminate\Http\Response96 */97 public function destroy($id)98 {99 //100 }101}
Our repository is now injected automatically into the controller. To make this work we need to run the following command:
$ composer dump-autoload
Running our tests should confirm that everything is still working:
1$ vendor/bin/phpunit2PHPUnit 5.5.4 by Sebastian Bergmann and contributors.3............ 12 / 12 (100%)45Time: 897 ms, Memory: 18.00MB67OK (12 tests, 46 assertions)
Let's do the same for the User model. First we implement our interface in app/Repositories/Interfaces/UserRepositoryInterface.php
:
1<?php23namespace AnimalFriend\Repositories\Interfaces;45interface UserRepositoryInterface {6 public function all();78 public function findOrFail($id);910 public function create($input);11}
Next we create our repository at app/Repositories/EloquentUserRepository.php
:
1<?php23namespace AnimalFriend\Repositories;45use AnimalFriend\User;6use AnimalFriend\Repositories\Interfaces\UserRepositoryInterface;7use JWTAuth;8use Hash;910class EloquentUserRepository implements UserRepositoryInterface {1112 private $user;1314 public function __construct(User $user)15 {16 $this->user = $user;17 }1819 public function all()20 {21 return $this->user->all();22 }2324 public function findOrFail($id)25 {26 return $this->user->findOrFail($id);27 }2829 public function create($input)30 {31 $user = new $this->user;32 $user->email = $input['email'];33 $user->name = $input['name'];34 $user->password = Hash::make($input['password']);35 $user->save();3637 // Create token38 return JWTAuth::fromUser($user);39 }40}
Note how we've moved much of the logic for creating a user into the create()
method, and we return the token, not the user model. This makes sense as right now we only ever want to get a token back when we create a user. Later that may change, but there's nothing stopping us adding a new method to implement that behaviour alongside this.
Then we update app/Http/Controllers/UserController.php
to use our repository:
1<?php23namespace AnimalFriend\Http\Controllers;45use Illuminate\Http\Request;67use AnimalFriend\Http\Requests;8use AnimalFriend\Repositories\Interfaces\UserRepositoryInterface as User;9use JWTAuth;10use Hash;1112class UserController extends Controller13{14 private $user;1516 public function __construct(User $user) {17 $this->user = $user;18 }1920 /**21 * Display a listing of the resource.22 *23 * @return \Illuminate\Http\Response24 */25 public function index()26 {27 //28 }2930 /**31 * Show the form for creating a new resource.32 *33 * @return \Illuminate\Http\Response34 */35 public function create()36 {37 //38 }3940 /**41 * Store a newly created resource in storage.42 *43 * @param \Illuminate\Http\Request $request44 * @return \Illuminate\Http\Response45 */46 public function store(Request $request)47 {48 // Validate request49 $valid = $this->validate($request, [50 'email' => 'required|email|unique:users,email',51 'name' => 'required|string',52 'password' => 'required|confirmed'53 ]);5455 // Create token56 $token = $this->user->create($request->only(57 'email',58 'name',59 'password'60 ));6162 // Send response63 return response()->json(['token' => $token], 201);64 }6566 /**67 * Display the specified resource.68 *69 * @param int $id70 * @return \Illuminate\Http\Response71 */72 public function show($id)73 {74 //75 }7677 /**78 * Show the form for editing the specified resource.79 *80 * @param int $id81 * @return \Illuminate\Http\Response82 */83 public function edit($id)84 {85 //86 }8788 /**89 * Update the specified resource in storage.90 *91 * @param \Illuminate\Http\Request $request92 * @param int $id93 * @return \Illuminate\Http\Response94 */95 public function update(Request $request, $id)96 {97 //98 }99100 /**101 * Remove the specified resource from storage.102 *103 * @param int $id104 * @return \Illuminate\Http\Response105 */106 public function destroy($id)107 {108 //109 }110}
And add a new binding in app/Providers/AppServiceProvider.php
:
1<?php23namespace AnimalFriend\Providers;45use Illuminate\Support\ServiceProvider;67class AppServiceProvider extends ServiceProvider8{9 /**10 * Bootstrap any application services.11 *12 * @return void13 */14 public function boot()15 {16 //17 }1819 /**20 * Register any application services.21 *22 * @return void23 */24 public function register()25 {26 $this->app->bind(27 'AnimalFriend\Repositories\Interfaces\PetRepositoryInterface',28 'AnimalFriend\Repositories\EloquentPetRepository'29 );30 $this->app->bind(31 'AnimalFriend\Repositories\Interfaces\UserRepositoryInterface',32 'AnimalFriend\Repositories\EloquentUserRepository'33 );34 }35}
Note that we bind the two sets separately - this allows Laravel to figure out which one maps to which.
Let's run our tests to make sure nothing is broken:
1$ vendor/bin/phpunit2PHPUnit 5.5.4 by Sebastian Bergmann and contributors.34............ 12 / 12 (100%)56Time: 956 ms, Memory: 18.00MB78OK (12 tests, 46 assertions)
Now that we've got our repositories in place, we're no longer tightly coupled to Eloquent, and have a more flexible implementation which is easier to test.
Separating our models from our JSON with Fractal
Another problem with our API is that our representation of our data is tightly coupled to our underlying implementation of our models. We therefore can't change our models without potentially changing the data returned by the API. We need to separate our representation of our data from our actual model so that we can more easily specify the exact data we want to return, regardless of the underlying database structure.
Enter Fractal. From the website:
Fractal provides a presentation and transformation layer for complex data output, the like found in RESTful APIs, and works really well with JSON. Think of this as a view layer for your JSON/YAML/etc.
In other words, Fractal lets you specify the format your data will take in one place so that it's easier to return that data in a desired format. We'll use Fractal to specify how we want our API responses to be formatted.
Install Fractal with the following command:
$ composer require league/fractal
Then amend the classmap in composer.json
:
1 "autoload": {2 "classmap": [3 "database",4 "app/Repositories",5 "app/Transformers"6 ],7 "psr-4": {8 "AnimalFriend\\": "app/"9 }10 },
Then create the folder app/Transformers
and run composer dump-autoload
. We're now ready to write our first transformer. Save this as app/Transformers/PetTransformer.php
:
1<?php23namespace AnimalFriend\Transformers;45use AnimalFriend\Pet;6use League\Fractal;78class PetTransformer extends Fractal\TransformerAbstract9{10 public function transform(Pet $pet)11 {12 return [13 'id' => (int) $pet->id,14 'name' => (string) $pet->name,15 'type' => (string) $pet->type,16 'available' => (bool) $pet->available,17 'picture' => (string) $pet->picture18 ];19 }20}
The transform
method specifies how we want to represent our objects with our API. We can return only those attributes we want to expose, and amend the structure as we see fit. We could easily represent relations in whatever manner we see fit, whereas before we needed to amend our queries to return the data in the right format, which would potentially be cumbersome.
Now let's amend PetController.php
to use this:
1<?php23namespace AnimalFriend\Http\Controllers;45use Illuminate\Http\Request;67use AnimalFriend\Http\Requests;8use AnimalFriend\Repositories\Interfaces\PetRepositoryInterface as Pet;9use AnimalFriend\Transformers\PetTransformer;10use League\Fractal;11use League\Fractal\Manager;1213class PetController extends Controller14{15 private $pet, $fractal;1617 public function __construct(Pet $pet, Manager $fractal) {18 $this->pet = $pet;19 $this->fractal = $fractal;20 }2122 /**23 * Display a listing of the resource.24 *25 * @return \Illuminate\Http\Response26 */27 public function index()28 {29 // Get all pets30 $pets = $this->pet->all();3132 // Format it33 $resource = new Fractal\Resource\Collection($pets, new PetTransformer);34 $data = $this->fractal->createData($resource)->toArray();3536 // Send response37 return response()->json($data, 200);38 }3940 /**41 * Show the form for creating a new resource.42 *43 * @return \Illuminate\Http\Response44 */45 public function create()46 {47 //48 }4950 /**51 * Store a newly created resource in storage.52 *53 * @param \Illuminate\Http\Request $request54 * @return \Illuminate\Http\Response55 */56 public function store(Request $request)57 {58 //59 }6061 /**62 * Display the specified resource.63 *64 * @param int $id65 * @return \Illuminate\Http\Response66 */67 public function show($id)68 {69 // Get pet70 $pet = $this->pet->findOrFail($id);7172 // Format it73 $resource = new Fractal\Resource\Item($pet, new PetTransformer);74 $data = $this->fractal->createData($resource)->toArray();7576 // Send response77 return response()->json($data, 200);78 }7980 /**81 * Show the form for editing the specified resource.82 *83 * @param int $id84 * @return \Illuminate\Http\Response85 */86 public function edit($id)87 {88 //89 }9091 /**92 * Update the specified resource in storage.93 *94 * @param \Illuminate\Http\Request $request95 * @param int $id96 * @return \Illuminate\Http\Response97 */98 public function update(Request $request, $id)99 {100 //101 }102103 /**104 * Remove the specified resource from storage.105 *106 * @param int $id107 * @return \Illuminate\Http\Response108 */109 public function destroy($id)110 {111 //112 }113}
Note that by default, Fractal places our data inside a dedicated data
namespace. This is good because it leaves a place for us to put metadata such as pagination links, but it does mean our controller test has been broken. Let's fix it:
1<?php23use Illuminate\Foundation\Testing\DatabaseMigrations;45class PetControllerTest extends TestCase6{7 use DatabaseMigrations;89 /**10 * Test fetching pets when unauthorised11 *12 * @return void13 */14 public function testFetchingPetsWhenUnauthorised()15 {16 // Create a Pet17 $pet = factory(AnimalFriend\Pet::class)->create([18 'name' => 'Freddie',19 'type' => 'Cat',20 ]);21 $this->seeInDatabase('pets', ['type' => 'Cat']);2223 // Create request24 $response = $this->call('GET', '/api/pets');25 $this->assertResponseStatus(400);26 }2728 /**29 * Test fetching pets when authorised30 *31 * @return void32 */33 public function testFetchingPets()34 {35 // Create a Pet36 $pet = factory(AnimalFriend\Pet::class)->create([37 'name' => 'Freddie',38 'type' => 'Cat',39 ]);40 $this->seeInDatabase('pets', ['type' => 'Cat']);4142 // Create a User43 $user = factory(AnimalFriend\User::class)->create([44 'name' => 'bobsmith',45 'email' => 'bob@example.com',46 ]);47 $this->seeInDatabase('users', ['email' => 'bob@example.com']);4849 // Create request50 $token = JWTAuth::fromUser($user);51 $headers = array(52 'Authorization' => 'Bearer '.$token53 );5455 // Send it56 $this->json('GET', '/api/pets', [], $headers)57 ->seeJsonStructure([58 'data' => [59 '*' => [60 'id',61 'name',62 'type',63 'available',64 'picture'65 ]66 ]67 ]);68 $this->assertResponseStatus(200);69 }7071 /**72 * Test fetching pet when unauthorised73 *74 * @return void75 */76 public function testFetchingPetWhenUnauthorised()77 {78 // Create a Pet79 $pet = factory(AnimalFriend\Pet::class)->create([80 'name' => 'Freddie',81 'type' => 'Cat',82 ]);83 $this->seeInDatabase('pets', ['type' => 'Cat']);8485 // Send request86 $response = $this->call('GET', '/api/pets/'.$pet->id);87 $this->assertResponseStatus(400);88 }8990 /**91 * Test fetching pet which does not exist92 *93 * @return void94 */95 public function testFetchingPetDoesNotExist()96 {97 // Create a User98 $user = factory(AnimalFriend\User::class)->create([99 'name' => 'bobsmith',100 'email' => 'bob@example.com',101 ]);102 $this->seeInDatabase('users', ['email' => 'bob@example.com']);103104 // Create request105 $token = JWTAuth::fromUser($user);106 $headers = array(107 'Authorization' => 'Bearer '.$token108 );109110 // Send it111 $this->json('GET', '/api/pets/1', [], $headers);112 $this->assertResponseStatus(404);113 }114115 /**116 * Test fetching pet when authorised117 *118 * @return void119 */120 public function testFetchingPet()121 {122 // Create a Pet123 $pet = factory(AnimalFriend\Pet::class)->create([124 'name' => 'Freddie',125 'type' => 'Cat',126 ]);127 $this->seeInDatabase('pets', ['type' => 'Cat']);128129 // Create a User130 $user = factory(AnimalFriend\User::class)->create([131 'name' => 'bobsmith',132 'email' => 'bob@example.com',133 ]);134 $this->seeInDatabase('users', ['email' => 'bob@example.com']);135136 // Create request137 $token = JWTAuth::fromUser($user);138 $headers = array(139 'Authorization' => 'Bearer '.$token140 );141142 // Send it143 $this->json('GET', '/api/pets/'.$pet->id, [], $headers)144 ->seeJsonStructure([145 'data' => [146 'id',147 'name',148 'type',149 'available',150 'picture'151 ]152 ]);153 $this->assertResponseStatus(200);154 }155}
We're also going to amend our test settings to use the array backend for the cache, as this does not require any external dependencies, but still allows us to tag our cache keys (I'll cover that in a future instalment). Change the cache settings in phpunit.xml
as follows:
<env name="CACHE_DRIVER" value="array"/>
Let's run our tests to make sure everything's fine:
1$ vendor/bin/phpunit2PHPUnit 5.5.4 by Sebastian Bergmann and contributors.34............ 12 / 12 (100%)56Time: 859 ms, Memory: 18.00MB78OK (12 tests, 44 assertions)
At present our User
controller doesn't actually return anything, and the auth only ever returns the token, so it's not worth while adding a transformer now.
Wrapping up
That ends this lesson. We haven't added any functionality, but we have improved the design of our API, and we're now ready to develop it further. As usual, the backend repository has been tagged as lesson-4
.
Next time we'll start adding the additional functionality we need to our API.