Monday, 12 December 2016

Create REST services in PHP

How to create REST services in PHP

Let's say we have our application directory structure like this

- api
- - request.php
- - .htaccess

Edit .htaccess file which is under api directory with below code


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ request.php?_url=$1 [QSA,NC,L]
</IfModule>

Every request comes with any pattern starting with "/api" will go to our "request.php"


This will handle any rest pattern with /api/*



Our request.php will look like

<?php

session_start();

$_url = $_GET['_url'];


switch ($_url) {
    case "products" :
        echo "All Products";
        break;
    case "product" :
        echo "One Product";
        break;
    case "product/save" :
        $request_body = file_get_contents('php://input');
        $data = json_decode($request_body);
        echo "Data saved to DB";
        break;
    default :
        header($_SERVER["SERVER_PROTOCOL"] . "404 Not Found", true, 404);
}


Write me back in comment, if have any query.


Wednesday, 9 November 2016

Redirect request using htaccess without changing the browser url

It's very simple if you know what's happening behind the line.

Lets see the requirement first, we have a url www.example.com/product/product1-page
Lets and we want to forward request to www.example.com/product?page=product1

Simply create a htaccess file under "PRODUCT" directory and copy below two lines

RewriteEngine on
RewriteRule ^/?(.*)-page ?page=$1 [L]

Explanation

^  - start matching request from here
(.*) - will become our variable $1
-page - request ending with -page
[L] - make this last rule


Once done, you can see the browser will show URL as www.example.com/product/product1-page

but underline request will to www.example.com/product?page=product1




Thursday, 13 October 2016

Publish angular 2 app to NPM

Prerequisites: Knowledge about angular 2 and it's configuration files.

First clone the project git@github.com:deshak9/Demo.git

1. First of all, we will look into JSearch, here I will just explain what require for publishing our library to NPM

2. About JSearch/src/jsearch.service.ts

  • A simple class
3. About JSearch/jsearch.ts
export {JSearch} from './src/jsearch.service';
  • This is the line which exports our library, which can be imported by other end developers like blow 

import { JSearch } from 'jsearch/jsearch';

4. About JSearch/package.json

"typings": "./jsearch.d.ts"
  • This line in package.json tell about the typings configuration so that compiler can perform autocomplete.

To test it locally, run following commands.

This command will download all dependencies
npm install

This command will compile all .ts files and generate .map, .js and .d.ts files
npm run app

This command will generate jsearch-1.0.0.tgz in root folder
npm pack

Now you should be able to see jsearch.js, jsearch.d.ts files in root folder along with jsearch.ts file


Now you are ready with your library to be published to NPM, but wait, first testing it locally will be great Idea.

So here you go

First of all run following commands,

npm install

Download our own library from local JSearch project
npm install ../JSearch/jsearch-1.0.0.tgz --save

You must see jsearch library in node_module directory.

1. We will go through JSearchTest

2. In JSearchTest/systemjs.config.js we have added this line so that we can import this library in our components
'jsearch' : 'npm:jsearch',

3. This change will ensure proper loading of jsearch library at the browser.

jsearch: {
    defaultExtension: "js"},


run the following command and you should see the message printed in you browser console

npm start


If everything goes right, run following command to publish your library to NPM server.

npm publish 

Sunday, 31 July 2016

Angular 2 example app

I have built a very basic chess application without any rules and  restriction using Angular 2



How to run it

1. First you must need to install Node js
    https://nodejs.org/en/download/

2. Set path in Environment variable for Node js home directory (ex : C:\Program Files\nodejs\)

3. Clone the directory from Git

4. Open command prompt on this cloned location.

5. Write a command ( if you already have node_module then you can skip this step)
    npm install

6. Write a command
    npm start


You should see your application started on localhost browser
Default port is "3000"

The way ahead :-
I will keep updating and adding more functionality to this application. Leave a comment if have any question.

Edit 1 :-

Updated the code with Chess Rules