我在 Wordpress 中編寫了一個插件,它應該處理所有其余的 API 請求。在每個請求中,$token都應該被解析,我必須檢查我的數據庫,這是否正確。我的問題是我想在父類中處理身份驗證,我的意思是我有 2 個類。一個authentication和另一個是handler從其父級擴展的。authentication例如,如果請求是/get/pictures/,我必須在authentication類中檢查是否設置了令牌然后處理其請求,否則返回false。我怎樣才能先解析$request到父類 add_action('rest_api_init', function(){ register_rest_route('myapi/v1', '/get/pictures/', array( 'method' => 'GET', 'callback' => array(new Handler(), 'get_pictures') )); }); // in Authentication.php class Authentication { public function check_token_valid(){ //check if token exist in the `$request` } } //in handler.php class Handler extends Authentication { public function get_pictures($request){ // do some stuff } }
2 回答

侃侃無極
TA貢獻2051條經驗 獲得超10個贊
你可以這樣使用
add_action('rest_api_init', function(){
register_rest_route('myapi/v1', '/get/pictures/',
array(
'method' => 'GET',
'callback' => array(new Handler(), 'get_pictures'),
'permission_callback' => function() {
return current_user_can( 'edit_others_posts' );
},
));
});
//in handler.php
class Handler extends Authentication {
public function get_pictures($request){
// do some stuff
}
}
- 2 回答
- 0 關注
- 124 瀏覽
添加回答
舉報
0/150
提交
取消