Skip to content

Commit

Permalink
updated the readme.md
Browse files Browse the repository at this point in the history
hopefully it will reflect more the intent of the package
  • Loading branch information
SamuelTissot committed Aug 17, 2018
1 parent 9bef65b commit f2f910e
Showing 1 changed file with 57 additions and 51 deletions.
108 changes: 57 additions & 51 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@ This is a fork of [Anthony Budd's](https://github.com/anthonybudd) [WP_Route](ht
### basic usage
```php

WP_Route::get('flights', 'listFlights');
WP_Route::post('flights/{flight}', 'singleFlight');
WP_Route::put('flights/{flight}/book/{date}', 'bookFlight');
WP_Route::delete('flights/{flight}/delete', 'deleteFlight');

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');
WP_Route::redirect('from/here', '/to/here', 301);

// if you want to take into account the parameters when doing a path match
WP_Route::get('/flights/', 'listFlights', true);

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

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

```

Expand All @@ -34,68 +43,65 @@ WP_Route::redirect('from/here', '/to/here', 301);
Require WP_Route with composer

```
$ composer require samueltissot/WP_Route
$ composer require samueltissot/wp_route
```


# GET Started
Simply define a route by calling any of the static methods get(), post(), put(), patch(), delete() or any(). This will bind the specified URL to a callable. When a HTTP request bound for that URL is detected, WP_Route will call the callable.
# All Callback must accept a variable of type `RequestInterface`
a request oject is passed to the callable method

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

### examples
```php
WP_Route::get('flights', 'listFlights');

// http://example.com/flights
function listFlights(){

// Your Code Here!

use samueltissot\WP_Route\RequestInterface;

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

# Parameters
If you need to extract route parameters from the request URI you can do this by wrapping the value to be extracted in curly brackets. The extracted values will be provided to the callable as function arguments as shown below.
```php
WP_Route::get('flights/{flight}', 'singleFlight');

function singleFlight($flight){
echo $flight; // 1
// or a simple function
function my_super_func(RequestInterface $req) {
// code goes here;
}
```

# Methods
### get($route, $callable)
### any(), post(), put(), patch(), delete()
All of these methods are used for binding a specific route and HTTP request method to a callable. The method any() will bind a route to a callable but will be HTTP method agnostic.
```php

WP_Route::get('flights', 'listFlights');
WP_Route::post('flights/{flight}', array('FlightController', 'singleFlight'));
// method inside class

function listFlights(){
// Your Code Here
}

Class FlightController{
public static function singleFlight($flight){
// Your Code Here
}
class MyAwesomeClass
{
public function wow(RequestInterface $req)
{
// code goes here;
}
}

```

### match($methods, $route, $callable)
If you want to bind a callable to multiple HTTP methods but you do not want to use any(), you can use match(). The first parameter must be an array of HTTP request methods. The arguments $route and $callable work the same as get().
```php
WP_Route::match(['get', 'post'], 'flights/{flight}/confirm', 'confirmFlight');

function confirmFlight($flight){
// Your Code Here
}
```
# The Request Object (RequestInterface)
a small helper object that provide usefull data about the request

### redirect($route, $redirect, $code = 301)
The redirect() method will redirect the user to the argument $redirect when they navigate to the route. To set a custom HTTP response code use the 3rd argument $code.
### the Interface
```php
WP_Route::redirect('open-google', 'https://google.com', 301);
```
interface RequestInterface
{
public function uri();

public function method();

public function pathVariables();

public function pathVariable($name);

public function parameters();

public function parameter($name);
}
```

0 comments on commit f2f910e

Please sign in to comment.