2016-10-07 31 views
-4
public function processApi(){  
    $func = strtolower(trim(str_replace("/","",$_SERVER['REQUEST_URI'])));  //echo $func; 

    if((int)method_exists($this,$func) > 0)   
    $this->$func();  
    else    
    $this->response('',404); 

    echo '404';  // If the method not exist with in this class, response would be "Page not found". 
} 

回答

0

您應使用此代碼

public function processApi(){  
     $func = strtolower(trim(str_replace("/","",$_SERVER['REQUEST_URI'])));  
     if(method_exists(get_called_class(), $func)) 
      { 
       $this->$func(); 
      }else{ 
       $this->response('method not exists',404); 
      } 
    } 

這種方法得到的類名


get_called_class()

在方法的返回真假方法

集類名存在與否

method_exists(get_called_class(),$ FUNC)

試試這個代碼

+0

感謝回答。 ,但仍然錯誤。 錯誤是:意外'' – jerry

+0

如果您使用MVC結構,所以得到錯誤,但使用核心oops沒有得到error.so如果您將使用MVC嘗試$ this-> router-> fetch_method();替換$ _SERVER ['REQUEST_URI'] –

+0

我使用了核心代碼。 – jerry