PHP code example of arafatkn / wrest

1. Go to this page and download the library: Download arafatkn/wrest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arafatkn / wrest example snippets




wrest()->setNamespace('my-plugin/v1');

wrest()->get('hello', function() {
    return 'Hello world';
});

wrest()->usingNamespace('my-plugin/v1', function($wrest) {
    // You can use both $wrest or wrest() here
    $wrest->get('greeting', function(WP_REST_Request $req) {
        return 'Hello world';
    });
});

wrest()->get('greeting', function(WP_REST_Request $req) {
    return 'Hello world';
});

wrest()->get('posts', $callback);
wrest()->post('posts', [$postController, 'store']);
wrest()->put($uri, $callback);
wrest()->patch($uri, $callback);
wrest()->delete($uri, $callback);
wrest()->any($uri, $callback); // All Routes GET, POST, PUT, PATCH, DELETE
wrest()->match(['GET', 'POST'], $uri, $callback);

wrest()->get('greeting', function() {
    return 'Hello world';
})->permission('manage_options');

wrest()->get('greeting', function() {
    return 'Hello world';
})->permission(function(WP_REST_Request $req) {
    return is_user_logged_in();
});

wrest()->get('/posts/{slug}', function(WP_REST_Request $request, $slug) {
    //
})->param('slug', '[A-Za-z]+');

wrest()->get('/user/{id}/{name}', function ($request, $id, $name) {
    //
})->param('id', '[0-9]+')->param('name', '[a-z]+');

wrest()->get('/user/{id}/{name}', function ($request, $id, $name) {
    //
})->param(['id' => '[0-9]+', 'name' => '[a-z]+']);

wrest()->get('/posts/{slug}', function(WP_REST_Request $request, $slug) {
    // Also Works. slug will contain all the characters between posts/ and next /.
});

wrest()->get('posts', function() => {});
wrest()->get('posts', 'getAllPosts'); // getAllPosts is a function.
wrest()->get('posts', 'PostController@getAll'); // getAll is static function.
wrest()->get('pages', [$pageController, 'getAll']); // getAll is non-static function.
wrest()->get('authors', [CommentController::class, 'getAll']); // getAll is static function.