无法从类实例注册REST路由

时间:2020-05-17 作者:Isaac Cohen

我正在为word Press编写一个OOP PHP插件。我有一个rest路由未注册的问题。。。

插件类:

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\';
    }
}
有人能解释一下如何用OOP PHP正确注册路由吗?

1 个回复
SO网友:Jacob Peattie

这里有几件事不对,没有一件与OOP真正相关。

首先,您似乎试图在插件激活时注册REST路由。这是不正确的。每个请求都需要注册路由,所以BusinessCustomersApiController->register_routes() 需要在每个请求上运行。你可以通过hooking 该函数用于rest_api_init.

问题是你使用add_action() 要添加该钩子,请在回调中进行。自从register_routes() 需要钩住才能跑,但钩住发生在里面register_routes(), 该函数将永远不会运行。

因此,要解决这个问题,您需要做两件事:

将呼叫移动到add_action() 转换为单独的方法

删除任何激活或停用相关方法。你没有使用它们init()) 到类中,并使用add_action() 进入这些

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\';
    }
}