Coder Social home page Coder Social logo

club-test-2's Introduction

HTTP PHP Logo

It's a simple HTTP request handling library, written in PHP. build on top of PHP 7.4.12 .

Quick Start

download exapmle with libary from here.

or

download the latest version of the library.

require_once 'PATH_OF_LIBRARY/http.php';

$app = new Http();

$app->get('/', function (Request $request, Response $response) {
    $response->send('Hello World!');
});

documentation:

Routing

Routes

Depending on the request method currentlly available class functions are:

get, post, put, delete, route.

// $app->get('/path', function());
// $app->post('/path', function());
// $app->put('/path', function());
// $app->delete('/path', function());
$app->get('/', function (Request $request, Response $response) {
    $response->send('Hello World!');
});
// $app->route('method', '/path', function());
$app->route('GET','/', function (Request $request, Response $response) {
    $response->send('Hello World!');
});

Router

This function is usefull to create nested routes.

// $app->router('/path', function() {
$app->get('/items', function ($uri) {

    $items = new HTTP_PHP();

    $items->get('/', function ($uri) {
        $items->send('Sending all items');
    });
    $items->get('/:id', function ($uri) {
        $items->send('send item');
    });
    $items->post('/:id', function ($uri) {
        $items->send('create item');
    });
    $items->put('/:id', function ($uri) {
        $items->send('update item');
    });
    $items->delete('/:id', function ($uri) {
        $items->send('delete item');
    });
});

Request

Body

// body(); returns the body of the request as a array
// body('key'); returns the value of the key
$app->get('/', function (Request $request, Response $response) {
    $body = $request->body();
    $response->json($body);
});

Query

// request query string : ?key=value
// result : ["key" => "value"]

// query(); returns the query of the request as a array
// query('key'); returns the value of the key
$app->get('/', function (Request $request, Response $response) {
    $query = $request->query();
    $response->json($query);
});

Params

// request params : /1234
// result : ["key"=>"1234"]

// params(); returns the params of the request as a array
// params('key'); returns the value of the key
$app->get('/:key', function (Request $request, Response $response) {
    $params = $request->params();
    $response->json($params);
});

Response

Send

$app->get('/', function (Request $request, Response $response) {
    $response->send('Hello World!');
});

Json

// jsson(); array as input
// json(); returns the body to client as json
$app->get('/', function (Request $request, Response $response) {
    $body = ['key' => 'value'];
    $response->json($body);
});

View

This function is usefull to render a view. sample file for views. file path reference use views/footer.html instead of views\footer.html. list of special php characters

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

    $templates=["views/header.html",
                "views/page1.html",
                "views/footer.html"];

    $data = ["page_title" => "Page 1",
             "page_content" => "Content of page 1"];

    // view([templates_path_array], [data_array])
    $response->view($templates, $data);
});

Contributing

This project is open-source, you can contribute to it by making a pull request or opening an issue.

People

Author of HTTP PHP is Chayan Sarkar

License

MIT

club-test-2's People

Contributors

apu-hub avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.