Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can use I use PHP as back-end for ionic framework? [closed]

Can anyone give an example of using php at the backend with Angular JS at the front-end in the Ionic Framework?

like image 978
Abhay Naik Avatar asked Apr 16 '15 05:04

Abhay Naik


People also ask

Can I use PHP in Ionic?

Yes. PHP can be used as a backend to interact with the database. Just like a regular Frontend-backend, requests and responses are in the form of JSON. Ionic can use the JSON responses generated by PHP.

Does Ionic need a backend?

It does not matter at all. Ionic apps communicate with their backend via APIs, so a REST or json or graphQL API. You can build those with any web framework and language out there, so use whatever you are most happy with.

Is Ionic front-end or backend?

Hybrid apps have many benefits over pure native apps, specifically in terms of platform support, speed of development, and access to 3rd party code. Think of Ionic as the front-end UI framework that handles all of the look and feel and UI interactions your app needs in order to be compelling.


Video Answer


1 Answers

Of Course !

Me and my partner just completed working on a IONIC App integrated with PHP as its backend.

Just like a regular Frontend-backend, requests and responses are in the form of JSON.

For getting started quickly, here is a sample code we built for ourselves :

send.php

<?php
// Prevent caching.
//header('Cache-Control: no-cache, must-revalidate');

// The JSON standard MIME header.
//header('Content-type: application/json');          

$data = array(
    "username" => "one",
    "email" => "[email protected]",
    "age"  => 22
    );

// Send the data.
echo json_encode($data);
?>

recieve.php

<?php

 /*
   * Collect all Details from Angular HTTP Request.
   */
    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    $usr = $request->email;
    $pass = $request->pass;

    echo "<h1> Username is : " . $usr . "<br /> and password is : ". $pass."</h1>"; //this will go back under "data" of angular call.
    /*
     * You can use $email and $pass for further work. Such as Database calls.
    */    

?>

Hope this helps you !

EDIT 1 :

The benefits of using PDO is over-rated. Read more about it here : http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059

I am assuming that you know about basic code for connecting to a database (http://www.w3schools.com/php/php_mysql_intro.asp).

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

As far as Angular coding is concerned, you may find the following links useful (sorry I don't have the angular code on this machine) :

http://codeforgeek.com/2014/07/angular-post-request-php/

http://www.cleverweb.nl/javascript/a-simple-search-with-angularjs-and-php/

http://serebrov.github.io/html/2013-05-24-angular-post-to-php.html

like image 173
Shalabh Avatar answered Nov 14 '22 21:11

Shalabh