PHP in HindiProgramming Language

How to Expose and Route to a Resource?

Program – How to Expose and Route to a Resource? If you want to provide access to resource and handle according to the HTTP method check code below.

Solution

The Solution is $_SERVER[‘REQUEST_METHOD’].

<?php

$request = explode('/', $_SERVER[PATH_INFO]);
$method = strtolower($_SERVER['REQUEST_METHOD']);

switch($method){
    case 'get':
        //for handling GET Request
        Break;
    case 'post':
        //for handling POST Request
        Break;
    case 'put'
        //for handling PUT Request
        Break;
    case 'delete':
        //for handling DELETE Request
        Break;
    default:
        //unimplemented method
    http_response_code(405);
}

 

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button
Close