2011-12-27 4 views
0

MVCアプリケーションを作成しようとしていますが、現在はBootstrapファイルで作業しています。私はURLを取得してそれを分解し、Controller MethodとMethodの引数にパーツを割り当てます。しかし、私はメソッドに複数の引数を渡す方法を見つけることができません。私はこのような二番目の配列を作成するための引数URLを分割してMVCで部品を渡す

個人用サイト/ NEWUSER /ログイン/ USER_NAME/user_pass

newuser -> Controler of the site 
login -> currently used method 

user_name -> first argument 
user_pass -> second_argument 
例えば

$url = "mysite/newuser/login/user_name/user_pass"; 

$path = expload('/',$url); 


$this->controler = $path[0]; 
$this->method = $path[1]; 

// Set the substring path as method properties 
if (isset($path[2])) { 

    $this->url_sub_path = $path[2]; 

    $sub_path = explode('/', $this->url_sub_path); 
    if (isset($sub_path)) { 

     $this->model_properties = $sub_path; 

私はコントローラにセットを割り当てます

$site_controler = $this->controler; 
include CONTROLER.$site_controler . '.php'; 

$new_instans = new $site_controler(); 

しかし、問題はここにある:私は、彼らが配列されているURLのプロパティを渡す必要があり

public function login($user_name,$user_pass){ 
    // some code 
} 

と:

$site_method = $this->model; 
$new_instans->{$site_method}($this->model_properties); 

$this->model_properties機能がある場合は、配列

です私は私の関数で2つの変数を持っています 考え方は、配列を変数に変換することです

それとも、この機能を試してみてください、私のモデルに

答えて

0

あなたが言うように、場合、$this->model_propertiesはあなたがすることができる配列であり、二つのものの一つ。

ケース1:関数宣言を維持し、関数を呼び出す前に配列の要素にアクセスします。

login()機能(それの宣言を維持する):

public function login($user_name,$user_pass){ 
// some code 
} 

関数を呼び出すには、この操作を行います。

$array = $this->model_properties; 
$param1 = $array[0]; //The numeric index may vary, depending on how this array was populated 
$param2 = $array[1]; 
$new_instans->{$site_method}($param1, $param2); 

ケース2:受信する関数の宣言を変更します配列内の配列の要素にアクセスします。

login()機能、宣言変更:

$new_instans->{$site_method}($this->model_properties); 

を個別にどのバージョンのあなたは:あなたはすでにやっているよう単に、配列を渡す関数を呼び出すには

public function login($arrayParams){ 
    //Access the parameters like this 
    $param1 = $arrayParams[0]; //The numeric index may vary, depending on how this array was populated 
    $param2 = $arrayParams[1]; 

    //The rest of your code... 
} 

をあなたの問題を解決することを選んでください、重要な部分はこれです:

$param1 = $array[0]; 
$param2 = $array[1]; 

ここで、インデックス01の配列の要素の内容を変数に代入し、それらの値を独立して扱うことができます。

関連する問題