<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: pardeep</title>
    <description>The latest articles on DEV Community by pardeep (@pardip_trivedi).</description>
    <link>https://dev.to/pardip_trivedi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F64590%2F17d8156a-9811-477f-8688-35393381799a.jpg</url>
      <title>DEV Community: pardeep</title>
      <link>https://dev.to/pardip_trivedi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pardip_trivedi"/>
    <language>en</language>
    <item>
      <title>Connect Laravel with Firebase Real Time Database</title>
      <dc:creator>pardeep</dc:creator>
      <pubDate>Thu, 31 Jan 2019 12:54:49 +0000</pubDate>
      <link>https://dev.to/pardip_trivedi/connect-laravel-with-firebase-real-time-database-493b</link>
      <guid>https://dev.to/pardip_trivedi/connect-laravel-with-firebase-real-time-database-493b</guid>
      <description>&lt;p&gt;Secure and efficient data retrieval is one of the fundamental requirements for a good app. Fortunately, developers have a number of options (for instance, framework level tools and secure DBMS) for implementing this requirement.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Firebase?
&lt;/h2&gt;

&lt;p&gt;Firebase was founded in 2012 as a real-time architecture for syncing data between apps. It was acquired by Google in 2014. Originally, it was a part of Envolve, a real-time chat integration API. Overtime, the developers realized that the system was being used to more than just text messages. Realizing the potential of the system, they come up with the idea of Firebase as a separate entity from Envolve.&lt;/p&gt;

&lt;p&gt;In this article , I will demonstrates how to connect Firebase Realtime Database in Laravel 5.5 (by using Admin SDK).&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;For the purpose of this tutorial, I assume that you have a Laravel application installed on a web server. My setup is:&lt;/p&gt;

&lt;p&gt;Laravel 5.5&lt;br&gt;
PHP 7.1&lt;br&gt;
Google Firebase Realtime Database&lt;/p&gt;

&lt;p&gt;Web server (Apache, NGINX or integrated PHP web server for testing).&lt;br&gt;
I have installed my Laravel app on a Cloudways managed server because it has a Laravel optimized dev stack and advanced &lt;a href="https://www.cloudways.com/en/laravel-hosting.php" rel="noopener noreferrer"&gt;PHP hosting&lt;/a&gt;. If you do not have an account on Cloudways, sign up for free, and check out the following GIF to setup the server and application in just a few clicks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffc0mwclglbobkmcgn8m4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffc0mwclglbobkmcgn8m4.gif" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How to Setup Firebase
&lt;/h2&gt;

&lt;p&gt;Let’s start with signing up for a Gmail account. In the next tab, open the Firebase page for database settings and click the Visit Console button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ftiio4cz0mvfybsgrthew.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ftiio4cz0mvfybsgrthew.png" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, in the next window, create a database project and provide information including project name. When done, click the Create Project button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F0miim1j29h6kx9julrsk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F0miim1j29h6kx9julrsk.png" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wait for the project to be created and then continue. You will be automatically redirected to the Firebase Dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fktqfab7qz9bi4t8qcthc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fktqfab7qz9bi4t8qcthc.png" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After successfully creating the project, go to the Database tab, click Rules to update read and write rules to true and then click the Publish button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fx1iilnp1jhy0d843t389.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fx1iilnp1jhy0d843t389.png" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Generate the API Key
&lt;/h2&gt;

&lt;p&gt;Firebase generates an  API key for your project that you can download and paste into http/Controller.&lt;/p&gt;

&lt;p&gt;Click the User and permissions tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4yqtk1jxcnidny4434zs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4yqtk1jxcnidny4434zs.png" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fhs768d2zny878d7z4wv9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fhs768d2zny878d7z4wv9.png" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, click Support account and then click the Create Key button&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ff1rtwgsjezklxl4m4l4b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ff1rtwgsjezklxl4m4l4b.png" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The key will be created in JSON format and downloaded automatically. You can copy the this file to the http/controller folder of Laravel.&lt;/p&gt;
&lt;h2&gt;
  
  
  Laravel Composer Require
&lt;/h2&gt;

&lt;p&gt;After setting up the Firebase database, go to the Laravel application and install kreait/firebase-php ^4.0. Open the SSH terminal and goto the public_root&lt;/p&gt;

&lt;p&gt;Enter the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require kreait/firebase-php ^4.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, you can specify the Firebase Admin SDK as a dependency in your project’s existing composer.json file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{

"require": {

"kreait/firebase-php": "^4.0"

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create the Controller
&lt;/h2&gt;

&lt;p&gt;After pasting the API Key in the Controller folder, go to the in SSH Terminal and type the following command to create the Controller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:controller FirebaseController
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, paste the following code in the Controller file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Kreait\Firebase;

use Kreait\Firebase\Factory;

use Kreait\Firebase\ServiceAccount;

use Kreait\Firebase\Database;

class FirebaseController extends Controller

{

//

public function index(){

$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/Laraveltesting-6aeda3a963f2.json');

$firebase = (new Factory)

-&amp;gt;withServiceAccount($serviceAccount)

-&amp;gt;withDatabaseUri('https://laraveltesting-bd2b9.firebaseio.com/')

-&amp;gt;create();

$database = $firebase-&amp;gt;getDatabase();

$newPost = $database

-&amp;gt;getReference('blog/posts')

-&amp;gt;push([

'title' =&amp;gt; 'Post title',

'body' =&amp;gt; 'This should probably be longer.'

]);

//$newPost-&amp;gt;getKey(); // =&amp;gt; -KVr5eu8gcTv7_AHb-3-

//$newPost-&amp;gt;getUri(); // =&amp;gt; https://my-project.firebaseio.com/blog/posts/-KVr5eu8gcTv7_AHb-3-

//$newPost-&amp;gt;getChild('title')-&amp;gt;set('Changed post title');

//$newPost-&amp;gt;getValue(); // Fetches the data from the realtime database

//$newPost-&amp;gt;remove();

echo"&amp;lt;pre&amp;gt;";

print_r($newPost-&amp;gt;getvalue());

}

}

?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember to replace the file path in fromJsonFile with your own file path. Also, replace the database URL with your database’s URL:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fta4y35qpuqx0b4yzzvz0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fta4y35qpuqx0b4yzzvz0.png" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Set up the Route
&lt;/h2&gt;

&lt;p&gt;Open  the Laravel routes/web.php file and update the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('/', function () {

return view('welcome');

});

Route::get('/phpfirebase_sdk','FirebaseController@index');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save all changes in the Laravel application and run your application to see the output in action:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F0w7efd5o49hnsqrklqum.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F0w7efd5o49hnsqrklqum.png" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Firebase Database
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flyz9z5alfncipa08rzan.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flyz9z5alfncipa08rzan.png" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope that by now you have a very good idea of using Firebase Realtime Database in Laravel project. To see my sample app in action, just check out the demo. If you need help, just drop a comment and I will get back to you&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>hosting</category>
      <category>firebase</category>
    </item>
    <item>
      <title>Create A REST API In Codeigniter With Basic Authentication</title>
      <dc:creator>pardeep</dc:creator>
      <pubDate>Wed, 28 Mar 2018 09:22:31 +0000</pubDate>
      <link>https://dev.to/pardip_trivedi/create-a-rest-api-in-codeigniter-with-basic-authentication-1e7f</link>
      <guid>https://dev.to/pardip_trivedi/create-a-rest-api-in-codeigniter-with-basic-authentication-1e7f</guid>
      <description>&lt;p&gt;The CodeIgniter RESTful API is a set of simple, modular helpers that can be easily integrated into your CodeIgniter projects, allowing you to create customizable RESTful APIs that suit all your needs. The package includes helpers for database manipulation, JSON Web Token generation, validation and signing, authentication methods, methods for accessing resources using a URI, as well as some utility methods that will help streamline your workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is REST API
&lt;/h2&gt;

&lt;p&gt;REST stands for Representational State Transfer.  A REST API is a web service which uses HTTP methods likes GET, PUT, POST, DELETE for data manipulation over the cross platforms.&lt;/p&gt;

&lt;p&gt;In this tutorial, I will demonstrate How you can create a REST API in Codeigniter. To create the API, I will use codeigniter-restserver, written by Phil Sturgeon and currently supported by Chris Kacerguis. I will also use the codeigniter-restclient library.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faekvvhtgvnj7hqhs42wy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faekvvhtgvnj7hqhs42wy.jpg" alt="Alt text" width="654" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The process of creating REST API in Codeigniter covers the following steps:&lt;/p&gt;

&lt;p&gt;Installation of Codeigniter framework on Cloudways&lt;br&gt;
Database and table(s) creation&lt;br&gt;
Setup libraries and permissions&lt;br&gt;
Setup authentication and API key(s)&lt;br&gt;
Setup HTTP calls (GET, PUT, POST, DELETE)&lt;br&gt;
Test the HTTP calls.&lt;/p&gt;
&lt;h2&gt;
  
  
  Install Codeigniter on Cloudways
&lt;/h2&gt;

&lt;p&gt;First sign up at Cloudways for a free account. Once the account is ready, login to your account and create a new server. Fill in the server and the application detail and select PHP Stack as your application. Next, enter application, server and project’s name.&lt;/p&gt;

&lt;p&gt;Note: You can host unlimited applications on a single server.&lt;/p&gt;

&lt;p&gt;Choose your provider (Google, Amazon, Vultr, DigitalOcean, Kyup), select server size according to your needs and click the Launch button. Check out the following GIF for more details:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffc0mwclglbobkmcgn8m4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffc0mwclglbobkmcgn8m4.gif" alt="Alt text" width="1024" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that your server and application is ready, open your server by clicking the server name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbxxl32wmfblxc9mdn5it.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbxxl32wmfblxc9mdn5it.png" alt="Alt text" width="800" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Login with the username and password provided in the Master Credentials area.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1r2sdum6m3z5d4ku4q5i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1r2sdum6m3z5d4ku4q5i.png" alt="Alt text" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you are connected to your server, go to the SSH terminal and type the following commands to install Codeigniter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd applications

cd applicationname/public_html

wget https://github.com/bcit-ci/CodeIgniter/archive/develop.zip

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdxjhcy5oum5s5iq5vyu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdxjhcy5oum5s5iq5vyu.png" alt="Alt text" width="800" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the download of the zip file finishes, unzip the file by using the following commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unzip develop.zip
mv CodeIgniter-develop codeigniter
rm index.php
rm develop.zip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, the installation is complete.&lt;/p&gt;

&lt;p&gt;Go to the Application tab on the Cloudways panel and select your application. Click the highlighted button (see the following image) to access the application. Remember to add /codeigniter to the URL and hit the Enter key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frss574fo60q6ama6o6oo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frss574fo60q6ama6o6oo.png" alt="Alt text" width="800" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create Database and Table(s)
&lt;/h2&gt;

&lt;p&gt;I will now create a simple database with a table named User. In order to create the database, go to the Application Management tab and launch the database manager.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc52fgz88dlc3p6773bj8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc52fgz88dlc3p6773bj8.png" alt="Alt text" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Type in the following command in the SQL command field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE `tbl_user` (
 `user_id` int(11) NOT NULL,
 `user_name` varchar(40) NOT NULL,
 `user_password` varchar(40) NOT NULL,
 `user_type` varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0e3ks3vzdacuiyoholqa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0e3ks3vzdacuiyoholqa.png" alt="Alt text" width="800" height="303"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up Libraries and Permissions
&lt;/h2&gt;

&lt;p&gt;First of all, download codeigniter-restserver and codeigniter-restclient libraries. Extract the contents and then drag and drop application/libraries/Format.php and application/libraries/REST_Controller.php files into the application’s directories.Remember to add require_once it at the top of the controllers in order to load them into the scope. Additionally, copy rest.php file from application/config in application’s configuration directory.&lt;/p&gt;

&lt;p&gt;Now create a file in the application’s root folder and name it .htaccess. Paste the following code in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Setup Authentication and API Key
&lt;/h2&gt;

&lt;p&gt;To setup authentication, first create the following tables in the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE `keys` (

 `id` int(11) NOT NULL,

 `key` varchar(40) NOT NULL,

 `level` int(2) NOT NULL,

 `ignore_limits` tinyint(1) NOT NULL DEFAULT '0',

 `is_private_key` tinyint(1) NOT NULL DEFAULT '0',

 `ip_addresses` text,

 `date_created` int(11) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE `logs` (

 `id` int(11) NOT NULL,

 `uri` varchar(255) NOT NULL,

 `method` varchar(6) NOT NULL,

 `params` text,

 `api_key` varchar(40) NOT NULL,

 `ip_address` varchar(45) NOT NULL,

 `time` int(11) NOT NULL,

 `rtime` float DEFAULT NULL,

 `authorized` varchar(1) NOT NULL,

 `response_code` smallint(3) DEFAULT '0'

) ENGINE=InnoDB DEFAULT CHARSET=utf8;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The table Keys will be used for storing the API key, and the Logs table will hold the logs of the request(s) received by the server.&lt;/p&gt;

&lt;p&gt;Now open up application / database.php and type in your hostname, dbname and password (available in the Application Access details).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcm4lns3ujvpavo7q9y7s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcm4lns3ujvpavo7q9y7s.png" alt="Alt text" width="456" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbavh5k6z8xj9wjv6jfa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbavh5k6z8xj9wjv6jfa.png" alt="Alt text" width="697" height="568"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next step is the setup of authentication. For this, open up application / autoload.php and change this line of code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$autoload['libraries'] = array( );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$autoload['libraries'] = array('database');

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now go to application / rest.php and set the following entities as shown&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$config['rest_enable_keys'] = TRUE;
$config['rest_logs_table'] = 'logs';
$config['rest_auth'] = 'basic';
$config['auth_source'] = '';

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The authentication is now ready. Nest up is the creation of the model and HTTP calls.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup HTTP Calls
&lt;/h2&gt;

&lt;p&gt;I will now create two files.&lt;/p&gt;

&lt;p&gt;Go to application/controllers and create a new file with the name of api.php. Paste the following code in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
defined('BASEPATH') OR exit('No direct script access allowed');

require(APPPATH.'/libraries/REST_Controller.php');
class Api extends REST_Controller
{

       public function __construct() {
               parent::__construct();
               $this-&amp;gt;load-&amp;gt;model('user_model');

       }    
       public function user_get(){
           $r = $this-&amp;gt;user_model-&amp;gt;read();
           $this-&amp;gt;response($r); 
       }
       public function user_put(){
           $id = $this-&amp;gt;uri-&amp;gt;segment(3);

           $data = array('name' =&amp;gt; $this-&amp;gt;input-&amp;gt;get('name'),
           'pass' =&amp;gt; $this-&amp;gt;input-&amp;gt;get('pass'),
           'type' =&amp;gt; $this-&amp;gt;input-&amp;gt;get('type')
           );

            $r = $this-&amp;gt;user_model-&amp;gt;update($id,$data);
               $this-&amp;gt;response($r); 
       }

       public function user_post(){
           $data = array('name' =&amp;gt; $this-&amp;gt;input-&amp;gt;post('name'),
           'pass' =&amp;gt; $this-&amp;gt;input-&amp;gt;post('pass'),
           'type' =&amp;gt; $this-&amp;gt;input-&amp;gt;post('type')
           );
           $r = $this-&amp;gt;user_model-&amp;gt;insert($data);
           $this-&amp;gt;response($r); 
       }
       public function user_delete(){
           $id = $this-&amp;gt;uri-&amp;gt;segment(3);
           $r = $this-&amp;gt;user_model-&amp;gt;delete($id);
           $this-&amp;gt;response($r); 
       }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, go to application/models and paste the following code in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

defined('BASEPATH') OR exit('No direct script access allowed');



/**

*

*/

class User_model extends CI_Model

{

public function read(){



       $query = $this-&amp;gt;db-&amp;gt;query("select * from `tbl_user`");

       return $query-&amp;gt;result_array();

   }



   public function insert($data){



       $this-&amp;gt;user_name    = $data['name']; // please read the below note

       $this-&amp;gt;user_password  = $data['pass'];

       $this-&amp;gt;user_type = $data['type'];



       if($this-&amp;gt;db-&amp;gt;insert('tbl_user',$this))

       {    

           return 'Data is inserted successfully';

       }

         else

       {

           return "Error has occured";

       }

   }



   public function update($id,$data){



      $this-&amp;gt;user_name    = $data['name']; // please read the below note

       $this-&amp;gt;user_password  = $data['pass'];

       $this-&amp;gt;user_type = $data['type'];

       $result = $this-&amp;gt;db-&amp;gt;update('tbl_user',$this,array('user_id' =&amp;gt; $id));

       if($result)

       {

           return "Data is updated successfully";

       }

       else

       {

           return "Error has occurred";

       }

   }



   public function delete($id){



       $result = $this-&amp;gt;db-&amp;gt;query("delete from `tbl_user` where user_id = $id");

       if($result)

       {

           return "Data is deleted successfully";

       }

       else

       {

           return "Error has occurred";

       }

   }



}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing the HTTP Calls
&lt;/h2&gt;

&lt;p&gt;To test the HTTP calls of the API, I will use Postman.Go to the Postman, Set the method to GET , then set the authentication and API key as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7jmqx8sa7fzeunn5b47w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7jmqx8sa7fzeunn5b47w.png" alt="Alt text" width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa2jxcttsfizic3i25o8w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa2jxcttsfizic3i25o8w.png" alt="Alt text" width="800" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now to test the POST request, set the request to POST and add the authentication and API key. Fill in the variables as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjxoyx7xagzshg1pmmuu1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjxoyx7xagzshg1pmmuu1.png" alt="Alt text" width="800" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, I will test the PUT request. Pass the id in the 3rd segment of the URL, set the request to PUT, set the authentication and the API key and fill in the parameters as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhiy5i80zs9pv9y863i2g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhiy5i80zs9pv9y863i2g.png" alt="Alt text" width="800" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To test the DELETE request, pass the id in the 3rd segment of the URL, set the request to DELETE, set the authentication and the API key and fill in the parameters as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq1lu1g6i4o3t16uvaw5o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq1lu1g6i4o3t16uvaw5o.png" alt="Alt text" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;So, this is how i can create Rest API in PHP. I hope it was helpful for you.If you need any help with the code or the idea of implementing your own RESTful API in Codeigniter, do leave a comment below.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>codeigniter</category>
      <category>hosting</category>
    </item>
  </channel>
</rss>
