Designing and developing a successful RESTful API is mostly very difficult. There are a lot of aspects to designing and writing a successful RESTful API; for example, securing and limiting the API. In this chapter, we’ll focus on the basics of REST with coding a simple Movies and Actors API with Laravel. We’ll make some JSON endpoints behind a basic authentication system, and will also learn a few Laravel 4 tricks. We’ll cover the following topics in this Tutorial:

  • Creating and migrating the users database
  • Configuring the users model
  • Adding sample users
  • Creating and migrating the movies database
  • Creating a movie model
  • Adding sample movies
  • Creating and migrating the actors database
  • Creating an actor model
  • Assigning actors to movies
  • Understanding the authentication mechanism
  • Querying the API

 

Creating and migrating the users database

We assume that you have already defined database credentials in the database.php file located at config/. For this application, we need a database. You can create a new database by simply running the following SQL command, or basically you can use your database administration interface such as phpMyAdmin:

CREATE DATABASE laravel_dunebook

After successfully creating the database for the application, first we need to generate an application key for our application. As you know from the previous chapters, this is necessary for the security and authentication class of our application. To do this, first open your terminal, navigate to your project folder, and run the following command:

php artisian generate:key

If no error occurs, we should edit the authentication class’ configuration file. For using Laravel’s built-in authentication class, we need to edit the configuration file, auth.php, which is located at config/. This file contains several options for the authentication facilities. If you need to change the table name, and so on, you can perform the changes in the auth.php file. By default, Laravel comes with a users model; you can see the User.php file that is located at app. With Laravel , we need to define which fields can be filled in our User model. Let’s edit app/User.php and add the “fillable” array:

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'users';

  /**
   * The attributes excluded from the model's JSON form.
   *
   * @var array
   */
  protected $hidden = array('password');

  // Specify which attributes should be mass-assignable
   protected $fillable = array('email', 'password');

  /**
   * Get the unique identifier for the user.
   *
   * @return mixed
   */
  public function getAuthIdentifier()
  {
    return $this->getKey();
  }

  /**
   * Get the password for the user.
   *
   * @return string
   */
  public function getAuthPassword()
  {
    return $this->password;
  }

  /**
   * Get the e-mail address where password reminders are sent.
   *
   * @return string
   */
  public function getReminderEmail()
  {
    return $this->email;
  }

}


Basically we need two columns for our RESTful API users, they are:

  • email: This column stores the author’s e-mail ID
  • password: This column is for storing the author’s password

Now we need several migration files to create the users table and add an author to our database. To create a migration file, give a command such as the following:

php artisan make:migration create_users_table --table=users 

Open the migration file that was created recently and located at database/migrations/. We need to edit the up() function as follows:

  public function up()
  {
    Schema::create('users', function(Blueprint $table)
    {
      $table->increments('id');
      $table->string('email');
      $table->string('password');
      $table->timestamps();
    });
  }

After editing the migration file, please run the migrate command:

php artisian migrate

As you know, the command creates the users table and its columns. If no error occurs, check the laravel_api database for the users table and the columns.

Advertisements