2013-10-21 42 views
0

想象我有一個加載的控制器的URL,如果名稱相匹配(目前嘿嘿忽略任何這方面的安全問題)

public function Load($controller, $action = "Index") 
    {            
     require_once("Controllers/" . $controller . "Controller.php"); 
     $controllerName = $controller . "Controller"; 
     $loadedController = new $controllerName(); 
     $actionName = "ActionResult_" . $action; 
     $loadedController->$actionName(); 
    } 

現在想象一下,我想日誌形式發送其$ _ POST細節如上推出的接收控制器的參數:

<?php 
    class ExcelUploadController extends Controller 
    { 
     public function ActionResult_Login($username = NULL, $password = NULL) 
     { 
      // The parameters need to be mapped to the $_POST parameters names probably from the Load method somewhere and pumped in to the $loadedController->$actionName(); 
      $post_username = $username; 
      $post_password = $password; 
      $this->ReturnView("ExcelUpload/Index"); 
     } 
    } 
?> 

也使不要緊什麼樣的順序參數聲明,它匹配基於函數的參數$ _POST鍵。

我該如何去做這件事,有什麼想法?

所以要澄清,如果這沒有意義。該方法可能是這個樣子:

public function Load($controller, $action = "Index") 
    {            
     require_once("Controllers/" . $controller . "Controller.php"); 
     $controllerName = $controller . "Controller"; 
     $loadedController = new $controllerName(); 

     $actionName = "ActionResult_" . $action; 

     $checkIfPostData = $_POST; 
     if(isset($checkIfPostData)) 
     { 
      // Do some funky wang to map the following $loadedController->$actionName(); 
      // with the username and password or any other $_POST keys so that in the calling method, I can grab hold of the $_POST values 

     } 

     $loadedController->$actionName(); 
    } 
+0

我使用的方法是將關聯數組作爲單個參數並添加保障條件來匹配按鍵。 –

+0

在接收方法中,你的意思是?任何例子的傢伙? – Jimmyt1988

+1

@JamesT你應該將用戶輸入抽象爲某種類型的Request類實例,然後將其作爲單個參數傳遞給方法:public function postLogin(Request $ request){..'你可能會發現[this] (http://stackoverflow.com/a/13396866/727208)發佈* [無恥自我推銷] *有些有用。 –

回答

1

什麼你正在尋找的是call_user_func_array()

編輯,回覆評論: 你有兩個選擇:重寫所有的功能,使它們只接受一個陣列()作爲參數,你解析數組值。有點挑剔,但在某些情況下可能有用。或者你可以請求一個函數所需的參數:

// This will create an object that is the definition of your object 
$f = new ReflectionMethod($instance_of_object, $method_name); 
$args = array(); 
// Loop trough params 
foreach ($f->getParameters() as $param) { 
    // Check if parameters is sent through POST and if it is optional or not 
    if (!isset($_POST[$param->name]) && !$param->isOptional()) { 
     throw new Exception("You did not provide a value for all parameters"); 
    } 
    if (isset($_POST[$param->name])) { 
     $args[] = $_POST[$param->name]; 
    } 
    if ($param->name == 'args') { 
     $args[] = $_POST; 
    } 
} 
$result = call_user_func_array(array($instance_of_object, $method_name), $args); 

這樣你的數組將被正確構造。 你也可以添加一些具體的處理,無論參數是否可選(我猜你可以從我給你的代碼中理解如何去做)

+0

你碰巧知道如何映射鍵通過,以便無論哪個鍵傳遞的順序,在接收函數中變量名稱將足以匹配回調函數的關鍵? – Jimmyt1988

+0

請參閱編輯我的迴應 – Bibear

+0

請注意,您可以使用$ _REQUEST來獲取'$ _GET'和'$ _COOKIE'的內容,而不是'$ _POST'。您可能也想要考慮來自GET請求的參數。 – Fred

0

由於數據通過POST發送你不需要任何參數傳遞給你的方法:

class ExcelUploadController extends Controller { 

    private $userName; 
    private $login; 

    public function ActionResult_Login() { 
     $this->userName = $_POST['username']; 
     $this->login = $_POST['login']; 
    } 
} 

不要忘記消毒和驗證用戶輸入!

+0

事情是,我希望他們作爲參數傳遞我的親愛的朋友。這是一個性感的小MVC框架,我想在幕後驗證等。 你知道symfony和代碼如何點燃它。雖然謝謝!很有責任 – Jimmyt1988

+0

在MVC中,通過解析$ _GET超全局數組中的URL,將參數傳遞給動作方法。但是,這不是$ _GET請求,這是$ _POST請求。 –

+0

call_user_func_array()似乎工作!與發佈數據..所以現在我必須映射它們:D – Jimmyt1988