How to Build Web Application Signup with Codeigniter 3.1.0
This tutorial is about How to Build Web Application Signup with Codeigniter 3.1.0
Introduction
This tutorial is to guide developers creating simple Mesosfer Web Application for user sign up with Codeigniter 3.1.0 based on mesosfer docs.
Prerequisites
To follow this tutorial, you will need to prepare these items below :
- Sign up for an account in mesosfer cloud platform then create application to get applicationId.
- Install composer.
- Download source framework codeigniter version 3.1.0 from this site.
Getting Started
To get our web application working well, you will need to have an applicationId from Mesosfer cloud platform. Login and go to Apps –> then click App Setting like the picture below.
Perpare & Setup Environment
Download source framework from codeigniter site then deploy to your web server (apache2, nginx, or others). Edit file application/config/autoload.php and change configuration like code below :
1 2 3 4 5 6 |
add item url in helper from : $autoload['helper'] = array(); to be : $autoload['helper'] = array('url'); |
Update $config[composer] on file application/config/config.php
1 2 3 4 5 |
change this line : $config['composer_autoload'] = FALSE; to be : $config['composer_autoload'] = './vendor/autoload.php'; |
Add Guzzle Package
Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Install on your codeigniter using composer like command below :
1 2 |
go to root folder application then run command this : composer require guzzlehttp/guzzle |
Prepare your own code
You have to create controller first. For example User.php on application/controller, then fill code below :
1 2 3 4 5 6 7 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class User extends CI_Controller { } |
Add static variable for appId, appKey, and instance of guzzle object. Then define new instance guzzle on construct class. Don’t forget to fill appId based on your applicationId in mesosfer cloud platform.
1 2 3 4 5 6 7 8 |
private $client = null; private $appId = 'Your appId'; private $appKey = 'Your appKey'; function __construct() { parent::__construct(); $this->client = new \GuzzleHttp\Client(); } |
First, we make method login as navigation to register view page.
1 2 3 4 |
public function register() { $this->load->view('pages/register'); } |
Then create directory pages in application/views and add file register.php on that directory. There is gonna be like this “application/views/pages/register.php”. Create form register inside that file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<html> <head> <title></title> </head> <body> <form action="<?php echo site_url('user/signup'); ?>" method="POST"> <table> <tr> <td>Firstname</td> <td><input type="firstname" name="firstname"></td> </tr> <tr> <td>Lastname</td> <td><input type="lastname" name="lastname"></td> </tr> <tr> <td>Email</td> <td><input type="email" name="email"></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password"></td> </tr> <tr> <td><input type="submit" value="submit"></td> </tr> </table> </form> </body> </html> |
If you access it from browser, It will be like the picture below:
Then let’s create signup method that handle action from register.
1 2 3 4 5 6 7 8 9 10 |
public function signup() { $firstname = $this->input->post('firstname'); $lastname = $this->input->post('lastname'); $email = $this->input->post('email'); $password = $this->input->post('password'); $respAuth = $this->client->request('POST', 'https://api.mesosfer.com/api/v2/users', ['json' => ['firstname' => $firstname, 'lastname' => $lastname, 'email' => $email, 'password' => $password],'headers' => ['X-Mesosfer-AppId' => $this->appId, 'X-Mesosfer-AppKey' => $this->appKey] ]); } |
If you want to display response result from Mesosfer RestAPI, we can var_dump variable $auth like script below :
1 2 3 4 5 |
$bodyAuth = (string) $respAuth->getBody(); $auth = json_decode($bodyAuth); echo "<pre>"; var_dump($auth); echo "</pre>"; |
Result of json response :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
object(stdClass)#46 (1) { ["result"]=> object(stdClass)#44 (4) { ["createdAt"]=> string(24) "2016-10-04T13:00:20.401Z" ["objectId"]=> string(10) "F8NaHXmh9N" ["sessionToken"]=> string(34) "r:04fb93eb9b4cf4c057bb53534c05d5e6" ["updatedAt"]=> string(24) "2016-10-04T13:00:20.747Z" } } |
If you look at your user data in Mesosfer cloud platform, It will look like the picture below :
Let’s try and share to your community. Download complete code here.
Conclusion
In this tutorial we’ve tried to create simple module signup user with Mesosfer and codeigniter. By using Mesosfer you will reduce your development process. Mesosfer also provide some other platform of SDK such as mobile application, game, and others so you can transfer your data in another platform easily. Mesosfer helps you connect your project to the cloud. Cut off the development process so you can straight into the delivery process instead stumble on the complicated infrastructure.