PHP code example of dbout / wp-module-rest-api

1. Go to this page and download the library: Download dbout/wp-module-rest-api 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/ */

    

dbout / wp-module-rest-api example snippets


use Dbout\WpRestApi\RouteLoader;

// One folder
$loader = new RouteLoader(__DIR__ . '/src/Api/Routes');

// Multiple folders
$loader = new RouteLoader([
    __DIR__ . '/themes/my-theme/api'
    __DIR__ . '/src/Api/Routes',
]);

// You can also use pattern
$loader = new RouteLoader(__DIR__ . '/src/Modules/*/Api/Routes');

$loader->register();



use Dbout\WpRestApi\Attributes\Route;
use Dbout\WpRestApi\Attributes\Action;

#[Route(
    namespace: 'app/v2',
    route: 'document/(?P<documentId>\d+)'
)]
class Document 
{

    #[Action(Method::GET)]
    public function get(\WP_REST_Request $request): \WP_REST_Response
    {
        // Add your logic 
        $id = $request->get_param('documentId');
       
        return new \WP_REST_Response([
            'success' => true,
        ]);
    }

   #[Action(Method::DELETE)]
    public function delete(\WP_REST_Request $request): \WP_REST_Response
    {
        // Add your logic 
        $id = $request->get_param('documentId');
        
        return new \WP_REST_Response([
            'success' => true,
        ]);
    }
}

#[Action([Method::GET, Method::POST, Method::PUT])]
public function execute(\WP_REST_Request $request): \WP_REST_Response
{
    // Add your logic 

}



use Dbout\WpRestApi\Attributes\Route;
use Dbout\WpRestApi\Attributes\Action;

#[Route(
    'app/v2',
    'document/(?P<documentId>\d+)'
)]
class Document
{

    #[Action(Method::GET)]
    public function get(int $documentId): \WP_REST_Response
    {
        // Add your logic

        return new \WP_REST_Response([
            'success' => true,
        ]);
    }
}