# 7. The server side of the project
I wrote server-side as a simple PHP script with a MySQL database. The same principles can be applied to any paradigm, framework or language.
For this tutorial I used XAMPP environment which runs on my computer.
The following is the MySQL code for generating the database table:
CREATE TABLE IF NOT EXISTS `cars` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`model` varchar(255) NOT NULL DEFAULT '',
`price` int (10) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Put the PHP scripts in a folder with the name of api/ on the server.
The .htaccess file is the configuration file of the Apache server, in which you specify two rules:
-
A rule to remove the PHP extension from the file names.
-
Headers to allow the Angular part of the application to transfer data and perform operations on the PHP server.
api/.htaccess
# Remove the php extension from the filename
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# Set the headers for the restful api
Header always set Access-Control-Allow-Origin http://localhost:4200
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
The header part of the .htacess file:
Header always set Access-Control-Allow-Origin "http://localhost:4200"
Instructs the server to give the Angular app the necessary privileges even though the web address is different.
The rest of the headers permit the exchange of data between the Angular side and the server side using the HTTP protocol methods:
GET
|
To retrieve the data about a single resource* or a list of resources
|
POST
|
To store a new resource
|
PUT
|
To update the data that is already found on the server
|
DELETE
|
To destroy a resource
|
* A resource is roughly equivalent to a single row in the database. In our app, every car is a resource.
The application conforms to the REST API architecture on which you can read in the following tutorial.
The connect.php script creates the connection with the database:
api/connect.php
<?php
// db credentials
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'angular_db');
// Connect with the database.
function connect()
{
$connect = mysqli_connect(DB_HOST ,DB_USER ,DB_PASS ,DB_NAME);
if (mysqli_connect_errno($connect)) {
die("Failed to connect:" . mysqli_connect_error());
}
mysqli_set_charset($connect, "utf8");
return $connect;
}
$con = connect();
The api/list.php script receives a GET request from the Angular side of the application and returns the list of cars stored in the database.
list.php
<?php
/**
* Returns the list of cars.
*/
require 'connect.php';
$cars = [];
$sql = "SELECT id, model, price FROM cars";
if($result = mysqli_query($con,$sql))
{
$cr = 0;
while($row = mysqli_fetch_assoc($result))
{
$cars[$cr]['id'] = $row['id'];
$cars[$cr]['model'] = $row['model'];
$cars[$cr]['price'] = $row['price'];
$cr++;
}
echo json_encode(['data'=>$cars]);
}
else
{
http_response_code(404);
}