Wednesday, November 20, 2019

Teknologi Web dan Membuat Simple PHP Service APP

Teknologi web

Perkembangan :
Website adalah kumpulan halaman berisi informasi pada suatu alamat url (bias ip atau dns) yang saling terhubung satu sama lain dan dapat dikases secara public melalui jaringan internet
Ada 4 perkembangan web sampai saat ini, yaitu :
  1. Web 1.0 static content, hosted on web
  2. Web 2.0 dynamic contents, ability to internet with web service
  3. Web 3.0 sematic web, seb service, app to app interctions.
  4. Web 4.0 electronic agents, ubiquitous web, human and machines interactions
Saat ini kita akan fokus terhadap web 3.0
Web 3.0 = mendesentrasikan service sesuai dengan fungsinya. Misalkan service khusus menangani pencarian dan lain lain.
Contoh : 
Sematic web menyesuaikan konten web berdasarkan ketertarikan pengguna
Web service memungkinkan setiap aplikasi dapat berkomunikasi dan bertukar data satu sama lain.

Arsitektur web service

Webservice adalah sebuah metode komunikasi antar aplikasi melalui jaringan
Komunikasi ini menggunakan XML/JSON, SOAP, WSDL, UDDI
Webservice terdiri dari 3 bagian utama
  1. Service provider
  2. Service requestor costumer
  3. Service broker
Web api adalah komunikasi antar interface berdasarkan pada REST
Web api dapat dibuat dan dikembangkan menggunakan Bahasa pemrogramman seperti java, .net, php dll.

Membuat Simple PHP Service APP

Membuat folder di xampp\htdocs\AdityaVR
Buat 3 buah folder dengan nama config, controller dan core

Buat sebuah file index.php di dalam folder AdityaVR 
Isi index.php dengan kode dibawah ini :

<?php

                ini_set('display_errors', 1);

                error_reporting(E_ALL);

                require __DIR__."/core/Bootstrap.php";

?>

Buatlah file Routes.php di dalam folder config, dan isikan dengan kode seperti dibawah ini :

<?php

                $routes = [

                                '/users' => 'Users.php',

                                '/' => 'Home.php'];

?>

Buat file Home.php di dalam folder controlles dengan kode seperti dibawah ini :

<?php

                $url = $_SERVER['REQUEST_URI'];

                $urlArr = explode("index.php", $url);

                if (count($urlArr) >= 2) {

                                $url = $urlArr[1];

                }

                if (strpos($url, "/") !== 0) {

                                $url = "/$url";

                }

                if ($url == '/' && $_SERVER['REQUEST_METHOD'] == 'GET') {

                                echo json_encode(array('service_name' => 'PHP service app', 'status' => 'Running'));

                }

?>

Buat juga User.php di dalam folder controller dengan kode seperti di bawah ini :

<?php

                $url = $_SERVER['REQUEST_URI'];

                $urlArr = explode("index.php",$url);

                if (count($urlArr) >= 2) {

                                # code...

                                $url = $urlArr[1];

                }

                if (strpos($url,"/")!==0) {

                                # code...

                                $url = "/$url";

                }

                if ($url == '/users' && $_SERVER['REQUEST_METHOD'] == 'GET') {

                                $users = array(

                                                array('id' => 1,'name' => 'aditya','email'=>'a1@gmail.com','address'=>'bandung','gender'=>'laki'),

                                                array('id' => 2,'name' => 'vajri','email'=>'a2@gmail.com','address'=>'bandung','gender'=>'laki'),

                                                array('id' => 3,'name' => 'ramadhan','email'=>'a3@gmail.com','address'=>'soreang','gender'=>'laki'),

                                                array('id' => 4,'name' => 'bobi','email'=>'a4@gmail.com','address'=>'gandasoli','gender'=>'laki'),

                                                array('id' => 5,'name' => 'petrik','email'=>'a5@gmail.com','address'=>'cpi','gender'=>'laki'),

                                                array('id' => 6,'name' => 'sendi','email'=>'a6@gmial.com','address'=>'disana','gender'=>'perempuan'),

                                );

                                echo json_encode($users);

                }

               

                if (preg_match("/users\/([0-9])+/",$url,$matches) && $_SERVER['REQUEST_METHOD'] == 'GET') {

                                # code...

                                $users = array(

                                                '1' => array('id' => 1,'name' => 'aditya','email'=>'a1@gmail.com','address'=>'bandung','gender'=>'laki'),

                                                '2' => array('id' => 2,'name' => 'vajri','email'=>'a2@gmail.com','address'=>'bandung','gender'=>'laki'),

                                                '3' =>  array('id' => 3,'name' => 'ramadhan','email'=>'a3@gmail.com','address'=>'soreang','gender'=>'laki'),

                                                '4' => array('id' => 4,'name' => 'bobi','email'=>'a4@gmail.com','address'=>'gandasoli','gender'=>'laki'),

                                                '5' =>  array('id' => 5,'name' => 'petrik','email'=>'a5@gmail.com','address'=>'cpi','gender'=>'laki'),

                                                '6' => array('id' => 6,'name' => 'sendi','email'=>'a6@gmial.com','address'=>'disana','gender'=>'perempuan'),

                                );

                                $user = $users[$matches[1]];

                                echo json_encode($user);

                }

                if (preg_match("/users\/name\/([0-9])+/",$url,$matches) && $_SERVER['REQUEST_METHOD'] == 'GET') {

                                # code...

                                $users = array(

                                                '1' => array('id' => 1,'name' => 'aditya'),

                                                '2' => array('id' => 2,'name' => 'vajri'),

                                                '3' => array('id' => 3,'name' => 'ramadhan'),

                                                '4' => array('id' => 4,'name' => 'bobi'),

                                                '5' => array('id' => 5,'name' => 'petrik'),

                                                '6' => array('id' => 6,'name' => 'sendi'),

                                );

                                $user = $users[$matches[1]];

                                echo json_encode($user);

                }

                if ($url == '/users/name' && $_SERVER['REQUEST_METHOD'] == 'GET') {

                                $users = array(

                                                array('id' => 1,'name' => 'aditya'),

                                                array('id' => 2,'name' => 'vajri'),

                                                array('id' => 3,'name' => 'ramadhan'),

                                                array('id' => 4,'name' => 'bobi'),

                                                array('id' => 5,'name' => 'petrik'),

                                                array('id' => 6,'name' => 'sendi'),

                                );

                                echo json_encode($users);

                }

                if (preg_match("/users\/email\/([0-9])+/",$url,$matches) && $_SERVER['REQUEST_METHOD'] == 'GET') {

                                # code...

                                $users = array(

                                                '1' => array('id' => 1,'email'=>'a1@gmail.com'),

                                                '2' => array('id' => 2,'email'=>'a2@gmail.com','address'=>'bandung','gender'=>'laki'),

                                                '3' => array('id' => 3,'email'=>'a3@gmail.com'),

                                                '4' => array('id' => 4,'email'=>'a4@gmail.com'),

                                                '5' => array('id' => 5,'email'=>'a5@gmail.com'),

                                                '6' => array('id' => 6,'email'=>'a6@gmial.com'),

                                );

                                $user = $users[$matches[1]];

                                echo json_encode($user);

                }

                if ($url == '/users/email' && $_SERVER['REQUEST_METHOD'] == 'GET') {

                                $users = array(

                                                array('id' => 1,'email'=>'a1@gmail.com'),

                                                array('id' => 2,'email'=>'a2@gmail.com','address'=>'bandung','gender'=>'laki'),

                                                array('id' => 3,'email'=>'a3@gmail.com'),

                                                array('id' => 4,'email'=>'a4@gmail.com'),

                                                array('id' => 5,'email'=>'a5@gmail.com'),

                                                array('id' => 6,'email'=>'a6@gmial.com'),

                                );

                                echo json_encode($users);

                }

               

?>

Dalam kode tersebut akan menampilkan 5 output, yaitu
  1. /users
  2. /users/id[0-9]
  3. /users/name
  4. /users/name/id[0-9]
  5. /users/email
Selanjutnya buat file Bootstrap di folder core dengan kode seperti dibawah ini :

<?php

                require __DIR__.'/Router.php';

                require __DIR__.'/../config/Routes.php';

                $router = new Router;

                $router->setRoutes($routes);

                $url = $_SERVER['REQUEST_URI'];

                require __DIR__."/../controller/".$router->getFilename($url);

?>

Yang terakhir buat file Router.php di dalam folder core dengan kode seperti dibawah ini:

<?php

                class Router{

                                private $routes = [];

                                function setRoutes(Array $routes) {

                                                $this->routes = $routes;

                                }

                                function getFilename(string $url) {

                                                foreach($this->routes as $route => $file) {

                                                                if (strpos($url, $route) !== false) {

                                                                                return $file;

                                                                }

                                                }

                                }

                }

?>

Jalankan aplikasi dengan cara
  1. Aktifkan xampp
  2. Buka browser
  3. Lalu ketikan http://localhost/[nama_folder]/index.php/
Jika berhasil akan terlihat seperti dibawah ini :





Friday, October 11, 2019

Image CRUD dengan Grocerycrud dan Codeigniter

Yang harus dilakukan adalah :


  • Download GroceryCrud image crud di grocerycrud.com/imgae-crud
  • Extract hasil downloadnya, lalu copy and replace pada folder codeigniter
  • Buka phpMyAdmin di http://localhost/phpmyadmin/
  • Buat database baru, contoh : db_image
  • lalu klik pada menu SQL
  • Pastekan examples_database.sql kedalam SQL hasilnya akan seperti berikut :


  • Selanjutna konfigurasi database db_image dengan codeigniter
  • Buka file database.php di xampp\htdocs\ImageCrud\application\config
  • isi username dengan root dan database dengan db_image

  • Buat controller di xampp\htdocs\application\controllers, Sebagai contoh : sibobi.php
  • ketikkan kode berikut :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class sibobi extends CI_Controller {

                function __construct()
                {
                                parent::__construct();
                               
                                /* Standard Libraries */
                                $this->load->database();
                                /* ------------------ */
                               
                                $this->load->helper('url'); //Just for the examples, this is not required thought for the library
                               
                                $this->load->library('image_CRUD');
                }
               
                function _example_output($output = null)
                {
                                $this->load->view('v_image.php',$output);         
                }
               
                function index()
                {
                                $this->_example_output((object)array('output' => '' , 'js_files' => array() , 'css_files' => array()));
                }             
               
                function example1()
                {
                                $image_crud = new image_CRUD();
                               
                                $image_crud->set_primary_key_field('id');
                                $image_crud->set_url_field('url');
                                $image_crud->set_table('example_1')
                                                ->set_image_path('assets/uploads');
                                               
                                $output = $image_crud->render();
                               
                                $this->_example_output($output);
                }
               
                function example2()
                {
                                $image_crud = new image_CRUD();
                               
                                $image_crud->set_primary_key_field('id');
                                $image_crud->set_url_field('url');
                                $image_crud->set_table('example_2')
                                ->set_ordering_field('priority')
                                ->set_image_path('assets/uploads');
                                               
                                $output = $image_crud->render();
               
                                $this->_example_output($output);
                }

                function example3()
                {
                                $image_crud = new image_CRUD();
               
                                $image_crud->set_primary_key_field('id');
                                $image_crud->set_url_field('url');
                                $image_crud->set_table('example_3')
                                ->set_relation_field('category_id')
                                ->set_ordering_field('priority')
                                ->set_image_path('assets/uploads');
                                               
                                $output = $image_crud->render();
               
                                $this->_example_output($output);
                }

                function example4()
                {
                                $image_crud = new image_CRUD();
               
                                $image_crud->set_primary_key_field('id');
                                $image_crud->set_url_field('url');
                                $image_crud->set_title_field('title');
                                $image_crud->set_table('example_4')
                                ->set_ordering_field('priority')
                                ->set_image_path('assets/uploads');
                                               
                                $output = $image_crud->render();
               
                                $this->_example_output($output);
                }
               
                function simple_photo_gallery()
                {
                                $image_crud = new image_CRUD();
                               
                                $image_crud->unset_upload();
                                $image_crud->unset_delete();
                               
                                $image_crud->set_primary_key_field('id');
                                $image_crud->set_url_field('url');
                                $image_crud->set_table('example_4')
                                ->set_image_path('assets/uploads');
                               
                                $output = $image_crud->render();
                               
                                $this->_example_output($output);                        
                }
}
  • Selanjutnya buat v_image.php di xampp\htdocs\application\views, lalu ketikkan kode berikut
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
    <?php
    foreach($css_files as $file): ?>
        <link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
    <?php endforeach; ?>
    <?php foreach($js_files as $file): ?>
        <script src="<?php echo $file; ?>"></script>
    <?php endforeach; ?>
    <style type='text/css'>
    body
    {
        font-family: Arial;
        font-size: 14px;
    }
    a {
        color: blue;
        text-decoration: none;
        font-size: 14px;
    }
    a:hover
    {
        text-decoration: underline;
    }
    </style>
    </head>
    <body>
        <div>
            <a href='<?php echo site_url('sibobi/example1')?>'>Example 1 - Simple</a> |
            <a href='<?php echo site_url('sibobi/example2')?>'>Example 2 - Ordering</a> |
            <a href='<?php echo site_url('sibobi/example3/22')?>'>Example 3 - With group id</a> |
            <a href='<?php echo site_url('sibobi/example4')?>'>Example 4 - Images with title</a> |
            <a href='<?php echo site_url('sibobi/simple_photo_gallery')?>'>Simple Photo Gallery</a>
        </div>
        <div style='height:20px;'></div> 
        <div>
            <?php echo $output; ?>
        </div>
    </body>
    </html> 


  • cobalah klik menu, dan masukan image yang lain ke dalam databasenya.

Thursday, October 10, 2019

CRUD Dengan CodeIgniter dan GroceryCrud

  • Yang harus dilakukan adalah
  • Download codeigniter
  • Download grocerycrud
  • Extract codeigniter dan grocerycrud
  • Copy and replace semua isi dari grocerycrud ke dalam codeigniter
  • Simpan folder codeigniter di directory xampp\htdocs
  • Buat sebuah database di localhost/phpMyAdmin dengan nama table files_db

  • Buatlah table dengan nama tb_file dengan columns sebanyak 7

  • Isi table dengan ketentuan sebagai berikut :

  • Atur konfigurasi database di codeigniter (\application\config\database.php)
  • Rubah username menjadi "root" dan database menjadi "files_db"

  • Buat sebuah controllers baru di \application\controllers dengan nama main.php
  • isi main.php dengan :
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {

    function __construct()
    {
        parent::__construct();

        /* Standard Libraries of codeigniter are required */
        $this->load->database();
        $this->load->helper('url');
        /* ------------------ */

        $this->load->library('grocery_CRUD');

    }

    public function index()
    {
        echo "<h1>Welcome to the world of Codeigniter</h1>";//Just an example to ensure that we get into the function
                die();
    }

    public function files()
    {
        $db_multimedia = new grocery_CRUD();
        $db_multimedia->set_table('tb_file');
        $output = $db_multimedia->render();

        $this->_example_output($output);       
    }

    function _example_output($output = null)

    {
        $this->load->view('v_main.php',$output);   
    }
}

  • Buat view di directory \application\views dengan nama v_main.php
  • isi v_main.php dengan :
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />

<?php
foreach($css_files as $file): ?>
    <link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />

<?php endforeach; ?>
<?php foreach($js_files as $file): ?>

    <script src="<?php echo $file; ?>"></script>
<?php endforeach; ?>

<style type='text/css'>
body
{
    font-family: Arial;
    font-size: 14px;
}
a {
    color: blue;
    text-decoration: none;
    font-size: 14px;
}
a:hover
{
    text-decoration: underline;
}
</style>
</head>
<body>
<!-- Beginning header -->
<!-- End of header-->
    <div style='height:20px;'></div> 
    <div>
        <?php echo $output; ?>

    </div>
<!-- Beginning footer -->
<div>Footer</div>
<!-- End of Footer -->
</body>
</html>

  •  Setelah selesai, buka browser lalu ketikkan
    http://localhost/Codeigniter/index.php/main/files
  • Maka akan muncul tampilan sebagai berikut :

  • Cobalah isi dengan tekan Add Record

  • Tekan Save and go back to list, dan hasilnya akan terlihat