亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將請求傳遞給父類

如何將請求傳遞給父類

PHP
慕村9548890 2021-09-05 16:06:40
我在 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

    }

 }


查看完整回答
反對 回復 2021-09-05
  • 2 回答
  • 0 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號