2017 © Pedro Peláez
 

library http

A minimalist http controller.

image

movicon/http

A minimalist http controller.

  • Thursday, June 28, 2018
  • by soloproyectos
  • Repository
  • 1 Watchers
  • 0 Stars
  • 16 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

HTTP

A library to process HTTP requests and print document. This library is part of the "Mimbre Framework" and allow us to implement a Controller and a View from the MVC design pattern. Specifically, this library contains two classes:, (*1)

  • HttpController: The base class of any controller
  • HttpView: The base class of any view

For know more details visit the Wiki pages., (*2)

Install

This library uses the Composer package manager. Simply execute the following command from a terminal:, (*3)

composer require movicon\http

The Controller

A 'controller' must extend the HttpController class. It processes HTTP requests and performs the corresponding actions. For example:, (*4)

header("Content-Type: text/plain; charset=utf-8");
require_once "path/to/vendor/autoload.php";
use movicon\http\HttpController;

class MyController extends HttpController {
  public function __construct() {
    // registers requests handlers
    $this->onOpen([$this, "open"]);
    $this->onGet([$this, "get"]);
    $this->on("DELETE", [$this, "delete"]);
    $this->onPost([$this, "post"]);
    $this->onClose([$this, "close"]);
  }

  // This request handler is called before any other handler, and it's
  // a good place to initialize resources, such as database connections, etc...
  public function open() {
    echo "Opening request...\n";
  }

  // This request handler processes 'GET requests'.
  public function get() {
    $param1 = $this->getParam("param1", ["default" => "Param 1"]);
    $param2 = $this->getParam("param2");

    echo "Processing GET request...\n";
  }

  // This request handler processes 'DELETE requests'.
  public function delete() {
    echo "Processing DELETE request...\n";
  }

  // This request handler processes 'POST requests'
  public function post() {
    $param1 = $this->getParam("param1", ["default" => "Param 1"]);
    $param2 = $this->getParam("param2");

    echo "Processing POST request...\n";
  }

  // This request handler is called after any other handler, and it's
  // a good place to close resources, such as database connections, etc....
  public function close() {
    echo "Closing request...\n";
  }
}

// Creates an instance of MyController and processes the request
$c = new MyController();
$c->processRequest();

The View

A 'view' is in charge of printing documents and must extends the HttpView class, which contains an abstract method called getDocument(). For example:, (*5)

header("Content-Type: text/plain; charset=utf-8");
require_once "path/vendor/autoload.php";
use movicon\http\HttpController;
use movicon\http\HttpView;

class MyController extends HttpController {
  public $username = "";

  public function __construct() {
    $this->onGet([$this, "get"]);
  }

  // This request handler processes 'GET requests'.
  public function get() {
    $this->username = $this->getParam("username");

    if (strlen($this->username) == 0) {
      throw new Exception("Username is required");
    }
  }
}

class MyView extends HttpView {
  public function __construct() {
    // The HttpView constructor accepts a controller as parameter
    parent::__construct(new MyController());
  }

  // This abstract method must be implemented
  public function getDocument() {
    return "Hi there, " . $this->controller->username . "!";
  }
}

// creates an instance and prints the document
$view = new MyView();
$view->printDocument();

Developer notes

# verifies that the source code conforms the current coding standards
phpcs --standard=phpcs.xml src

The Versions

28/06 2018

dev-master

9999999-dev

A minimalist http controller.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Gonzalo Chumillas

28/06 2018

2.0.0

2.0.0.0

A minimalist http controller.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Gonzalo Chumillas

14/06 2018

1.0.1

1.0.1.0

A minimalist http controller.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Gonzalo Chumillas

14/06 2018

1.0.2

1.0.2.0

A minimalist http controller.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Gonzalo Chumillas

13/06 2018

1.0.0

1.0.0.0

A minimalist http controller.

  Sources   Download

MIT

The Requires

  • php >=7.2.0

 

by Gonzalo Chumillas