0

I am writing a plugin in OOP PHP for word press I have a problem with rest routes not registering...

plugin class:

require_once plugin_dir_path( __FILE__ ) . 'inc/org-clients-repository.php';
require_once plugin_dir_path( __FILE__ ) . 'inc/org-clients-api-controller.php';
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

class BusinessCustomersPlugin
{
    private $_apiController = null;

    function onActivate(){
    $_apiController = new BusinessCustomersApiController();
    $_apiController->onActivate();
    $_repository = new BusinessCustomersRepository();

    $_repository->onActivate();
    }

    function onDeactivate() {

    }

    function onUninstall() {

    }

}

if ( class_exists( 'BusinessCustomersPlugin' ) ) {
    $businessCustomers = new BusinessCustomersPlugin();
}


// activation
register_activation_hook( __FILE__, array( $businessCustomers, 'onActivate' ) );

// deactivation
register_deactivation_hook( __FILE__, array( $businessCustomers, 'onDeactivate' ) );

class BusinessCustomersApiController
{
    public function __construct() {
        $this->namespace     = '/customers/org/v1/';
        $this->resource_name = 'posts';

    }
    public function register_routes(){


        register_rest_route(
            $this->namespace, 'test/', [
                'methods' => 'GET',
                'callback' => array($this,'test'),
            ]
        );

        register_rest_route(
            $this->namespace, 'add/', [
                'methods' => 'POST',
                'callback' => 'add_customer',
            ]
        );
        add_action('rest_api_init',[ $this, 'register_routes' ] );

    }
    /**
     *  onActivate
     */
    function onActivate()
    {
        $this->register_routes();
    }
     /**
     *  onDeactivate
     */
    function onDeactivate()
    {

    }
    /**
     *  onUninstall
     */
    function onUninstall()
    {

    }

    public   function test()
    {
    return 'test';
    }
}

can anyone explain how to register routes properly with OOP PHP?

1

There's several things wrong here, none really related to OOP.

Firstly, you seem to be attempting to register the REST routes on plugin activation. This is incorrect. Routes need to be registered for every request, so BusinessCustomersApiController->register_routes() needs to run on every request. You do this by hooking that function to the rest_api_init.

The problem there is that your use of add_action() to add that hook is inside the callback. Since register_routes() needs to be hooked to run, but the hooking happens inside register_routes(), the function will never run.

So, there's two things you need to do to fix this:

  1. Move the call to add_action() into a separate method.
  2. Run that method on every request.

Here's how I'd do it:

  1. Remove any activation or deactivation related methods. You're not using them.
  2. Add a new method (I'll call it init()) to the classes, and put any use of add_action() into those.
  3. Call the that method on the class instance after creating it.

The result will be something like this. Note that I've removed code that wasn't relevant to the issue, to demonstrate the solution:

class BusinessCustomersPlugin {
    function init(){
        $_apiController = new BusinessCustomersApiController();
        $_apiController->init();
    }
}

$businessCustomers = new BusinessCustomersPlugin();
$businessCustomers->init();

class BusinessCustomersApiController {
    public function __construct() {
        $this->namespace = '/customers/org/v1/';
    }

    public function init() {
        add_action( 'rest_api_init', [ $this, 'register_routes' ] );
    }

    public function register_routes() {
        register_rest_route(
            $this->namespace, 
            'test/',
            [
                'methods'  => 'GET',
                'callback' => [ $this, 'test' ],
            ]
        );
    }

    public function test() {
        return 'test';
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.