The previous day How to create REST API for Android app using PHP, Slim and MySQL – Day ½, we have learned fundamental concepts about REST API and preparing your development environment ready by installing the required tools needed. I hope everyone got good knowledge about REST and other technical areas. Also I am assuming that you got all the required tools installed.
Today we are going to learn how to setup a PHP project and writing the actual code for REST API. Also we’ll learn writing necessary SQL queries to perform database CRUD operations.
8. Starting PHP Project
As we all know IDEs make development process easier. So I recommend you use an IDE for developing the PHP project instead of using plain notepad. You can go for Eclipse, Aptana Studio, PhpStorm or Netbeans. But I personally felt very comfortable using Netbeans for PHP projects.
PHP Project directory structure
The following diagram will give you an idea about the directory structure of the project which we are going to develop now.
The following diagram will give you an idea about the directory structure of the project which we are going to develop now.
libs – All the third party libraries goes here. In our case we place Slim library here
include – All the helpers classes we build placed here
index.php – Takes care of all the API requests
.htaccess – Rules for url structure and other apache rules
include – All the helpers classes we build placed here
index.php – Takes care of all the API requests
.htaccess – Rules for url structure and other apache rules
Now let’s start the PHP project
1. Go to the directory where WAMP is installed. In general wamp will be installed in C:\wamp. (If you have installed any other software rather than WAMP, you should go to the directory recommended by that software).
2. As a first step we start with creating required directories. Inside wamp folder go to www folder (c:\wamp\www\) and create a folder named task_manager. This folder will be the parent directory of our project. Inside task_manager create two more folders named libs, include and v1.
3. Now the paste the Slim library inside libs folder. The download link for Slim is provided in previous part.
4. Normally Slim framework works when index.php includes in the url which makes url not well-formed. So using the .htacess rules we can get rid of index.php from the url and make some friendly urls. Inside v1 folder create a file named .htaccess and paste the following code. (Note that this file name shouldn’t include any additional extension in the name like .txt)
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L] |
8.1 Preparing Helper Classes
First we start writing set of helper classes required in this project. These helper classes provides necessary functions required to interact with the database.
5. Inside include folder create file named Config.php with following content. This file contains the entire project configuration like database connection parameters and other variables.
<?php /** * Database configuration */ define( 'DB_USERNAME' , 'root' ); define( 'DB_PASSWORD' , '' ); define( 'DB_HOST' , 'localhost' ); define( 'DB_NAME' , 'task_manager' ); define( 'USER_CREATED_SUCCESSFULLY' , 0); define( 'USER_CREATE_FAILED' , 1); define( 'USER_ALREADY_EXISTED' , 2); ?> |
6. Create another class named DbConnect.php This class file mainly takes care of database connection.
<?php /** * Handling database connection * * @author Ravi Tamada */ class DbConnect { private $conn ; function __construct() { } /** * Establishing database connection * @return database connection handler */ function connect() { include_once dirname( __FILE__ ) . './Config.php' ; // Connecting to mysql database $this ->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME); // Check for database connection error if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // returing connection resource return $this ->conn; } } ?> |
Encrypting the password
7. The best way to secure the user passwords is not store them as plain text, instead all the passwords should be encrypted before storing in db. The following class takes care of encrypting the user password. Create another file named PassHash.php and paste the following code.
7. The best way to secure the user passwords is not store them as plain text, instead all the passwords should be encrypted before storing in db. The following class takes care of encrypting the user password. Create another file named PassHash.php and paste the following code.
<?php class PassHash { // blowfish private static $algo = '$2a' ; // cost parameter private static $cost = '$10' ; // mainly for internal use public static function unique_salt() { return substr (sha1(mt_rand()), 0, 22); } // this will be used to generate a hash public static function hash( $password ) { return crypt( $password , self:: $algo . self:: $cost . '$' . self::unique_salt()); } // this will be used to compare a password against a hash public static function check_password( $hash , $password ) { $full_salt = substr ( $hash , 0, 29); $new_hash = crypt( $password , $full_salt ); return ( $hash == $new_hash ); } } ?> |
thanku :)
ReplyDeleteThank you for sharing a details post you can also check Alight motion pc apk
ReplyDeleteVery well explained. BTW, you can download Alight motion mod apk 2023 to create attractive videos.
ReplyDelete