Skip to content

Handling of HTTP Request Methods

Rosario Carvello edited this page Jan 14, 2018 · 16 revisions

Introduction

In the previous page, we saw how to pass values using methods and parameters of a controller. However, the Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP works as a request-response protocol between a client and server. A web browser may be the client, and an application on a computer that hosts a website may be the server.

Example: A client (browser) submits an HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and may also contain the requested content.

Two HTTP Request Methods: GET and POST

Two commonly used methods for a request-response between a client and server are GET and POST.

  • GET - Requests data from a specified resource
  • POST - Submits data to be processed to a specified resource

The GET Method

Note that the query string (name/value pairs) is sent in the URL of a GET request:

http:/localhost/hello_world.php?name=Mark&skill=Developer

The POST Method

Note that the query string (name/value pairs) is sent in the HTTP message body of a POST request:

POST /hello_world.php HTTP/1.1
Host: localhost
name=Mark&skill=Developer

Handling of GET and POST with WebMVC

PHP provides:

  • $_GET associative array to access all the sent information using the GET method.
  • $_POST associative array to access all the sent information using the POST method.

Inside a Controller, you can use both of these arrays for handling HTTP GET and/or POST requests.
In the following example, we added the method sayWhatYouGet to HelloWorld for showing you how using $_GET for handling HTTP GET requests:

<?php
namespace controllers;

use framework\Controller;

class HelloWorld extends Controller
{

    public function sayHello()
    {
        echo "Hello world";
    }
	
    public function sayHelloMessage($message)
    {
        echo "Hello $message";
    }

    /**
     * A simple method for showing of $_GET processing
     */
    public function sayWhatYouGet()
    {     
        echo "You get the following requests:<br>";
           foreach($_GET as $get_variable => $value) {
               echo "$get_variable = $value <br>";
           }
    }

    protected function cantSayHello()
    {
        echo "This method cannot be called from Url";
    }
}

Now, to execute the sayWhatYouGet method in conjunction with an HTTP GET request containing some data, type:

http://localhost/hello_world/say_what_you_get/?name=Mark&skill=Developer

You will get the following results:

You get the following requests:
name = Mark
skill = Developer

The management of $_POST array is similar: you need to replace it instead of $_GET in the code above. Then, to execute the sayWhatYouGet method in conjunction with an HTTP POST you need to use a tool like Linux CURL and type:

curl --data "name=Mark&skill=Developer" http://localhost/hello_world/say_what_you_get

Note: instead of using S_GET or $_POST you can also handle the $_REQUEST, the PHP associative array that by default contains the contents of $_GET, $_POST, and $_COOKIE.

Whats Next

In the next page, we expose some advantages deriving from the conjunction of OOP and WebMVC

Clone this wiki locally