awethemes/wp-http

The Missing WordPress HTTP Foundation

v1.0.1 2018-01-08 18:32 UTC

README

Provide HTTP Request & Response for WordPress inspired by Laravel.

Examples

<?php

use Awethemes\Http\Kernel;
use Awethemes\Http\Request;

function my_plugin_register_routes( $router ) {
    $router->get( '/hello/{user}', function( Request $request, $user ) {
        return [ 'hello' => $user ];
    });
}

function my_plugin_dispatch() {
    global $wp;

    if ( empty( $wp->query_vars['my-route'] ) ) {
        return;
    }

    $kernel = new Kernel;
        ->use_request_uri( $wp->query_vars['my-route'] )
        ->use_dispatcher( \FastRoute\simpleDispatcher( 'my_plugin_register_routes' ) )
        ->handle( Request::capture() );
}
add_action( 'parse_request', 'my_plugin_dispatch' );

Test your route:

> curl -X GET "http://yoursite.dev/index.php?my-route=/hello/david"

{"hello":"david"}