24 February 2018

How to develop PHP web service part 2:Routing and removing index.php name from URL


In these tutorials I am going to teach you how to go the next level of developing PHP web service or REST API. REST API has great demand at this time. Using REST API developer build database connection to once and can use with websites, Mobile development i.e Android and IOS app development

If you haven't read our prior post. Do read it

Project structure knowledge is very necessary for the development process. After downloading SLIM framework using composer in webAppTest folder, there will only one folder name vendor and composer.lock and composer.json you will see. rest are created by me.


PHP web service or REST API development step by step using SLIM framework

1. after downloading SLIM framework, You have to make one new folder and name it with the version like V1 or which you want.

2. Inside the created folder, create index.php file. In this file, we will write all PHP web service related code. 

code which will in index.php

//Constants.php file code as below
<?php

use \Psr\Http\Message\ServerRequestInterface as Request;  //line (1)

use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';  //line (2)

//require '../include/Constants.php';

//require '../include/DbOperation.php';

$app = new \Slim\App(['settings' => ['displayErrorDetails' => true]]); //line (3)

$app->get('/hello/{name}', function (Request $request, Response $response) {

    $name = $request->getAttribute('name');

    $response->getBody()->write("Hello, $name");

    return $response;

});//line (4)

$app->run();  //line (5)

?>

Explanation


line (1) 

the first line is using PHP feature of namespacing. you can read more about here. Namespacing is a way of giving uniqueness to class in PHP. we can use this class in another class using line (1). as keyword work as giving the other name to a class.


line (2)
Including a PHP file in the current PHP file using the require keyword. '../' send to the previous folder. like in cmd typing cd .. remove the current folder.

line (3)
Here code initializes slim framework App class. Which is responsible for accepting GET, POST, PUT, DELETE REST API verb. line(3) responsible to call the appropriate function. So it is the most important class in SLIM. Inside App, setting property allows any error shown to the user.

line(4)
you make get request.

line(5)
finally, call run method.

You can check this above code at the following URL
http://localhost/webAppTest/V1/index.php/hello/yourName


Remove index.php from URL in slim REST API development


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

copy above code and save it as .htaccess in index.php folder. It is so simple, isn't it?

now you can check above code from the browser by calling
http://localhost/webAppTest/V1/hello/yourName.

Because the REST API Request type is GET that's why we can check it from the browser. For POST, PUT, DELETE methods we use REST API clients like Insomnia, postman, etc.

Next tutorials on advanced REST API creation. Connecting to the database.
Stay tuned. Let me know, if you find something which can be done in better way.

No comments:

Post a Comment

Ads Inside Post