Laravel Lumen CRUD REST API with Angular ShoppingList Application -part 1

Adrian Sebuliba
4 min readAug 1, 2018
Buying from the shop

Intro

Before we get started, I assume you are a familiar with Lumen or Laravel and you just want to get the complete workflow of how to build a project that fully covers Create Read Update Delete (CRUD) plus JWT authentication in laravel lumen and that you want to understand how the whole thing (REST API) can be consumed with a client application written in say Angular or any other client written in Javascript library or framework.

In this part 1 of the two-part series of creating a shopping list application called shoppers, I will build a REST API in Lumen a PHP microframework from the same guys who brought to you laravel.

Before we get started, ensure that you have the following tools already installed on your local machine.

  • An IDE, preferably PHPStorm or Visual Studio Code.
  • Lumen micro-framework a “lightweight laravel”
  • PostgreSQL database
  • Postico a database management tool for mac
  • PHP installed
  • Postman (Rest client for testing the API)

Now let's get started.

Database Design:

Let's begin with creating the project

Creating Lumen Project

  • With composer installed on your computer.
  • Now open up your terminal and run the following command.
  • composer create-project laravel/lumen Shopper
  • In the above command, Shopper is the name of your project. You will find a folder with the same name in the directory where you ran the above command.

Now open Shopper in your favorite IDE preferably PHPStorm

Folder Structure

  • There are very many folders here but the most important ones for our application are
  • app: Here we will create a Models folder that will house our models that will act as a means for communicating or interfacing with the database easily.
  • app/http/controllers: This is a folder that will be hosting our controllers for Shopper.
  • app/http/middlewares: We will define the API security here.
  • migrations: This is where we will define the files that will represent our database schema. These files are called migrations and act kinda like version control for our tables.
  • routes: This is where endpoints for Shoppers API will be defined.

Setting Up Database:

For the database, we will

  • Create a database called shoppers in postico for Mac users or any other database administration tool for other platforms.
  • You do not need to create any tables. Just create a database. (I have created a database named shoppers).
  • Now within your project folder in your IDE open .env file, and modify it as below.

You are free to change the above .env according to your database configuration. The above file is only shown here for demo purposes.

  • Next, we will create a database schema in migrations.

Database Migrations

Migrations are blueprints for tables to be created or modified in a database.

Simple migrations can be created by using the command

php artisan make:migration <name_of_migration> when creating new tables. However, as time goes by there will arise the need to modify certain table(s) in our database which will involve creating new migration(s) to modify the said table(s)

  • Open your terminal or cmd at the root of your project

Creating Migrations

  • On the terminal and issue the following command.
php artisan make:migration create_users_table --create=users
  • It is very important that you begin by creating the users table schema because the other tables will be heavily dependant on it. Creating this schema first will save us future problems when running migrations
  • Next run
php artisan make:migration create_categories_table --create=categoriesphp artisan make:migration create_items_table --create=items
  • In that order. Notice how categories migrating came before items migrations. This ensures that you do not run into problems while running migrations if you happen to define foreign key constraints.
  • The new migration will be placed in database/migrations directory. For more info about migration visit Lumen/Laravel documentation.

Designing Database Schema

Now populate the above-created schemas as shown below:

xxxx_create_user_table.php:

xxxx_create_create_categories.php:

xxxx_create_items.php:

  • Now our database schema is ready. Now, let's run the migrations and create table in our Postgres database.

Making Migration:

  • In the terminal type php artisan migrate. And the database will be populated with four tables namely migration, users, categories and items table automatically.
  • Now check postico and you will see that the tables are created. See image below
Results after running migrations

Making Models

  • Models make interacting with the database a breeze. The Laravel Eloquent ORM will be used with no single line of SQL queries written to interact with the database.
  • Eloquent is not enabled in Lumen by default so, head over to your bootstrap/app.php file and uncomment the lines
  • $app->withEloquent();
  • $app->withFacades();
  • Now create a folder called Models in your app folder
  • Finally create User.php, Category.php, and Item.php inside the Models folder.

User.php:

Category.php:

Item.php

Creating URL EndPoints

  • Now within routes/web.php add the following code.

Controllers:

  • Now create all controllers inside app/http/controllers.
  • All controllers extend the class Controller.
  • Now do composer require illuminate/routing

AuthController.php

CategoriesController.php:

ItemsController.php:

Adding JWT Authentication:

For JWT authentication we are going to use the firebase/php-jwt library so

Run the following composer command in the terminal:

composer require firebase/php-jwt

Create a file JwtMiddleware.php under the middleware folder

Now in your bootstrap/app.php under register middleware add this line of code to register above middleware

app->routeMiddleware([‘jwt.auth’ => App\Http\Middleware\JwtMiddleware::class,]);

Now that everything is set up, we are ready to test our application in postman.

For details on how to set up everything and get started, go to the Github repo.

Thanks for reading :).

Part 2 of this series can be found here.

--

--