REST API CRUD Angular 7 application with PHP and MySQL back-end.

Related Post

  • No related post.

In this tutorial, you’ll create an example REST API CRUD Angular 7 application with PHP and MySQL back-end.

You will be creating a simple RESTful API that supports GET, POST, PUT and DELETE requests and allow you to perform CRUD operations against a MySQL database to create, read, update and delete records from a database.

For the application design, It’s a simple interface for working with vehicle insurance policies. For the sake of simplicity, you are only going to add the following attributes to the policies database table:

  • number which stores to the insurance policy number,
  • amount which stores the insurance amount.

 

This is of course far from being a complete database design for a fully working insurance system. Because at least you need to add other tables like employees, clients, coverage, vehicles and drivers etc. And also the relationships between all these entities.

In this tutorial we assume you have the following prerequisites:

  • The MySQL database management system installed on your development machine,
  • PHP installed on your system (both these first requirements are required by the back-end project),
  • Node.js 8.9+ and NPM installed in your system. This is only required by your Angular project.

You also need to have a working experience with PHP and the different functions that will be used to create the SQL connection, getting the GET and POST data and returning JSON data in your code.

You need to be familiar with TypeScript, a superset of JavaScript that’s used with Angular.

A basic knowledge of Angular is preferable but not required since you’ll go from the first step until your create a project that communicates with a PHP server.

 

Creating the PHP Application

Let’s start by creating a simple PHP script that connects to a MySQL database and listens to API requests then responds accordingly by either fetching and returning data from the SQL table or insert, update and delete data from the database.

Create a folder for your project:

You create the angular-php-app that will contain the full front-end and back-end projects. Next, you navigate inside it and create the backend folder that will contain a simple PHP script that implements a simple CRUD REST API against a MySQL database.

Next, navigate into your backend project and create an api folder.

Open the backend/api/database.php file and add the following PHP code step by step:

These lines are used to add response headers such as CORS and the allowed methods (PUT, GET, DELETE and POST).

Setting CORS to * will allow your PHP server to accept requests from another domain where the Angular 7 server is running from without getting blocked by the browser by reason of the Same Origin Policy. In development, you’ll be running the PHP server from localhost:8080 port and Angular from localhost:4200 which are considered as two distinct domains.

Next, add:

These variables hold the credentials that will be used to connect to the MySQL database and the name of the database.

Next Add

This will allow you to create a connection to the MySQL database using the mysqli extension.

That’s all for the database.php file

Implementing the Read Operation

Now, let’s implement the read operation. Open the backend/api/read.php file and add the following code:

This will fetch the list of policies from the database and return them as a JSON response. If there is an error it will return a 404 error.

Implementing the Create Operation

Let’s now implement the create operation. Open the backend/api/create.php file and add the following code:

Implementing the Update Operation

Open the backend/api/update.php file and add the following code:

Implementing the Delete Operation

Open the backend/api/delete.php file and add the following code:

In all operations, we first require the database.php file for connecting to the MySQL database and then we implement the appropriate logic for the CRUD operation.

Serving the PHP REST API Project

You can next serve your PHP application using the built-in development server using the following command

This will run a development server from the 127.0.0.1:8080 address.

Creating the policies SQL Table

Next create the policies SQL table with two number and amount columns:

Now, you are ready to send GET, POST, PUT and DELETE requests to your PHP server running from the 127.0.0.1:8080 address.

For sending test requests, you can use REST clients such as Postman or cURL before creating the Angular UI.

Leave your server running and open a new terminal.

Creating the Angular 7 Project

Now that you’ve created the RESTful API with a PHP script, you can proceed to create your Angular 8 project.

Leave a Comment