Coder Social home page Coder Social logo

wp_route's Introduction

WP_Route

CircleCI

This is a fork of Anthony Budd's WP_Route

Added features

  • small security fix
  • Named Paths Variable
  • renamed parameter to PathVariable
  • pass an Request object to the callable function
  • option to ignore query string

basic usage

WP_Route::get('/flights/', 'listFlights');
WP_Route::get('/buses/', 'listBuses');
WP_Route::post('/flights/{flight}/', 'singleFlight');
WP_Route::put('/flights/{flight}/book/{date}', 'bookFlight');
WP_Route::delete('/flights/{flight}/delete', 'deleteFlight');
WP_Route::any('flights/{flight}',   array('Class', 'staticMethod'));
WP_Route::patch('flights/{flight}', array($object, 'method'));
WP_Route::match(['get', 'post'],    'flights/{flight}/confirm', 'confirmFlight');

// if you want to take into account the parameters when doing a path match 
WP_Route::get('/flights/', 'listFlights', ["match" => "*"]);

// if you want to match one or more parameters
WP_Route::get('/flights/', 'listFlights', ["match" => ['param2', 'param2', ...]]);

// redirect
WP_Route::redirect('open-google', 'https://google.com', 301);

// close
WP_Route::get('flights/{flight}', function singleFlight(RequestInterface $req) {
    $req->pathVariable('flight');
}

the arguments list

[
    "parameters" => [
        'match' => [[parameter1] [, parameter2 [, ...]]],
        'no_match' => [[parameter1] [, parameter2 [, ...]]],
    ],
]

NOTE: an empty array means : apply to all

Installation

Require WP_Route with composer

$ composer require samueltissot/wp_route

All Callback must accept a variable of type RequestInterface

a request object is passed to the callable method

note: it is in the plans to be able to provide your custom RequestInterface class (PR accepted)

examples

use samueltissot\WP_Route\RequestInterface;

// an invocable class
class Controller
{
    public function __invoke(RequestInterface $req)
    {
        // code goes here;
    }
}


// or a simple function
function my_super_func(RequestInterface $req) {
    // code goes here;
}

// method inside class

class MyAwesomeClass
{
    public function wow(RequestInterface $req)
    {
        // code goes here;
    }
}

The Request Object (RequestInterface)

a small helper object that provide usefull data about the request

the Interface

interface RequestInterface
{
    public function uri();

    public function method();

    public function pathVariables();

    public function pathVariable($name);

    public function parameters();

    public function parameter($name);
}

wp_route's People

Contributors

anthonybudd avatar samueltissot avatar

Watchers

James Cloos 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.