RESTful API's are hard! There are a lot of aspects to designing and writing a successful one. For instance, some of the topics that you may find yourself handling include authentication, hypermedia/HATEOS, versioning, rate limits, an...
RESTful API's are hard! There are a lot of aspects to designing and writing a successful one. For instance, some of the topics that you may find yourself handling include authentication, hypermedia/HATEOS, versioning, rate limits, and content negotiation. Rather than tackling all of these concepts, however, let's instead focus on the basics of REST. We'll make some JSON endpoints behind a basic authentication system, and learn a few Laravel 4 tricks in the process.The AppLet's build an API for a simple Read-It-Later app. Users will be able to create, read, update and delete URLs that they wish to read later. Ready to dive in and get started?Install Laravel 4Create a new install of Laravel 4. If you're handy with CLI, try this quickstart guide. Otherwise, we have a video tutorial here on Nettuts+ that covers the process.We're going to first create an encryption key for secure password hashing. You can do this easily by running this command from your project root:$ php artisan key:generate
Alternatively, you can simple edit your app/config/app.php encryption key:/*
|--------------------------------------------------------------------------
| Encryption Key
|--------------------------------------------------------------------------
|
| This key is used by the Illuminate encrypter service and should be set
| to a random, long string, otherwise these encrypted values will not
| be safe. Make sure to change it before deploying any application!
|
*/
'key' => md5('this is one way to get an encryption key set'),
DatabaseOnce you have a working install of Laravel 4, we can get started with the fun. We'll begin by creating the app's database.This will only require two database tables:Users, including a username and passwordURLs, including a url and descriptionWe'll use Laravel's migrations to create and populate the database.Configure Your DatabaseEdit app/config/database.php and fill it with your database settings. Note: this means creating a database for this application to use. This article assumes a MySQL database.'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'read_it_later',
'username' => 'your_username',
'password' => 'your_password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
Create Migration Files$ php artisan migrate:make create_users_table --table=users --create
$ php artisan migrate:make create_urls_table --table=urls --create
These commands set up the basic migration scripts that we'll be using to create the database tables. Our job now is to fill them with the correct table columns.Edit app/database/migrations/SOME_DATE_create_users_table.php and add to the up() method:public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username')->unique();
$table->string('password');
$table->timestamps();
});
}
Above, we're setting a username (which should be unique), a password, as well as the timestamps. Save that, and now edit app/database/migrations/SOME_DATE_create_urls_table.php, and add to the up() method:public function up()
{
Schema::create('urls', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id');
$table->string('url');
$table->string('description');
$table->timestamps();
});
}
The only important note in this snippet is that we're creating a link between the url and users table, via the user_id field.Add Sample UsersWe can use Laravel's seeds to create a few sample users.Create a file within the app/database/seeds folder that has the same name as the table that it corresponds to; in our case, UserTableSeeder.php. Add:delete();
User::create(array(