GithubHelp home page GithubHelp logo

dimitribouteille / wp-module-rest-api Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 0.0 59 KB

πŸš€ Quickly add routes to the WordPress REST API.

Home Page: https://packagist.org/packages/dbout/wp-module-rest-api

License: MIT License

PHP 100.00%
api rest rest-api wordpress php wordpress-rest-api wp wordpress-starter wordpress-development wordpress-api

wp-module-rest-api's Introduction

WordPress Rest API

GitHub Release tests Packagist Downloads

WordPress module designed for developers that want to add routes to the WordPress Rest API in a few moments.

πŸ’‘ To simplify the integration of this library, we recommend using WordPress with one of the following tools: Bedrock, Themosis or Wordplate.

Documentation

If you want to know more about how the WordPress API works, you can read the WordPress documentation :)

Installation

Requirements

The server requirements are basically the same as for WordPress with the addition of a few ones :

Installation with composer

You can use Composer. Follow the installation instructions if you do not already have composer installed.

composer require dbout/wp-module-rest-api

In your PHP script, make sure you include the autoloader:

require __DIR__ . '/vendor/autoload.php';

Usage

Before creating your first route, you must initialize the module. It is advisable to add this code at the beginning of the functions.php file of your theme or in a mu-plugin.

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();

πŸ’‘ The module will automatically search for all classes that are in the folder and sub folder.

πŸ’‘ You can pass as the second argument of RouteLoader an option object: read the documentation.

Now you have initialized the module, you just need to create your first route in the routes folder.

<?php

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,
        ]);
    }
}

You just created 2 routes πŸŽ‰

  • GET:wp-json/app/v2/document/18
  • DELETE:wp-json/app/v2/document/18

The logic is extremely simple, you can use the following methods: GET, POST, PUT, PATCH and DELETE

If you need, you can define multiple methods for an action by passing a method array :

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

}

Callback arguments

If your route contains parameters, you can retrieve them as an argument for your function :

<?php

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,
        ]);
    }
}

πŸ’‘If your function contains a WP_REST_Request argument, the WP_REST_Request object will be passed as an argument.

Contributing

We encourage you to contribute to this repository, so everyone can benefit from new features, bug fixes, and any other improvements. Have a look at our contributing guidelines to find out how to raise a pull request.

wp-module-rest-api's People

Contributors

dimitribouteille avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

wp-module-rest-api's Issues

[BUG] Parser::findClassName return invalid className

With this class :

<?php

namespace Dbout\DendreoWebhook\Api\Routes\Webhook;

use Dbout\DendreoWebhook\Entities\DendreoEvent;
use Dbout\DendreoWebhook\Api\Exceptions\DendreoWebhookException;
use Dbout\DendreoWebhook\Logger\Logger;
use Dbout\DendreoWebhook\Enums\Webhook;
use Dbout\DendreoWebhook\Services\Builders\BuilderInterface;
use Dbout\WpRestApi\Attributes\Action;
use Dbout\WpRestApi\Attributes\Route;
use Dbout\WpRestApi\Enums\Method;
use Dbout\WpRestApi\Exceptions\RouteException;

#[Route(
    'app/v2',
    'dendreo/webhook'
)]
class Create
{
    /**
     * @param \WP_REST_Request $request
     * @return \WP_REST_Response
     * @throws DendreoWebhookException
     * @throws RouteException
     */
    #[Action(methods: Method::POST)]
    public function execute(\WP_REST_Request $request): \WP_REST_Response
    {
        throw new \Exception('Invalid builder class type.');
}

return Dbout\DendreoWebhook\Api\Routes\Webhook\type ...

Dbout\WpRestApi\Loaders\AnnotationDirectoryLoader::findClass() not working with named arguments

<?php

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,
        ]);
    }
}

findClass not working with this file ... route/Document returned :(

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.