簡體   English   中英

PHP MVC傳遞多個參數

[英]PHP MVC pass multiple parameters

由於我缺乏 PHP 編程經驗,我很難理解如何在 URL 中傳遞多個參數。

I have the following URL http://some.domain/examr/pages/listQuestions/1/OSPF where the objective is to pass the last 2 parameters to the listQuestions controller function, however, only one is being passed. URL 被重寫為$_GET['URL']

My framework has a file core.php file which holds the core class used to basically split the URL into an array (or should I say explode) and loads the methods and properties of the controller.

class Core {

protected $currentController = 'Pages';
protected $currentMethod = 'index';
protected $params = [];

public function __construct(){
    //print_r($this->getUrl());
    $url = $this->getUrl();
    
    //look in controllers for index 0
    if(!empty($url) && file_exists('../app/controllers/' .ucwords($url[0]). '.php')){
        //If exits set as current controller
        $this->currentController = ucwords($url[0]);
        // unset index 0
        unset($url[0]);
    }
    
    //require controller
    require_once '../app/controllers/' . $this->currentController . '.php';
    
    //instantiate controller class
    $this->currentController = new $this->currentController;
    
    // check for url index [1]
    
    if(isset($url[1])){
        if(method_exists($this->currentController, $url[1])){
            $this->currentMethod = $url[1];
            unset($url[1]);
        }
    }
    
    // get params
    $this->params = $url ? array_values($url) : [];
    //callback function
    call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
}

public function getUrl(){
    if(isset($_GET['url'])){
        $url = rtrim($_GET['url'], '/');
        $url = filter_var($url, FILTER_SANITIZE_URL);
        $url = explode('/', $url);
        return $url;
    }
}

}

我在$this->params上使用了print_r function ,我可以看到它正在獲取所有 URL 參數,到目前為止我想很好:-)

此外,我還有另一個文件,其中包含用於加載模型和視圖的基本 controller。

class Controller {
 //load models
 
 public function model($model){
     //require model file
     require_once '../app/models/' . $model . '.php';
     //instantiate model
     return new $model();
 }
 
 //load views
 public function view($view, $data = []){
     // check if view file exist
     if(file_exists('../app/views/' . $view . '.php')){
         require_once '../app/views/' . $view . '.php';
     } else {
         die('View does not exist' . $view);
        
     }
 }
 
 }

這一切都匯集在一個引導文件中,該文件加載核心和基礎 controller,該文件加載到 index.php 文件中並實例化 class 核心。

From the previous URL you can see that I have a controller named pages which is extending the base controller and would like to pass the last 2 URL params to the function listQuestions (which is wip).

public function listQuestions($params){
    print_r($params);
    $exam_id = $params;
    $questions = $this->dbModel->listQuestions($exam_id);
    $data = [
            'description' => 'exam details',
            'questions' => $questions
        ];
    if($questions != 0){
        $this->view('pages/listQuestions', $data);
    }
    
    print_r($questions);
}

我可以從print_r($params); 只傳遞了 1 個參數,我不知道我的錯誤在哪里,也不知道如何進一步解決這個問題。

我也有以下重寫規則

<IfModule mod_rewrite.c>
  Options -Multiviews
  RewriteEngine On
  RewriteBase /examr/public
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule  ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>

任何幫助表示贊賞。

call_user_func_array將數組作為參數並將其提取到單獨的 arguments 中。 所以在這種情況下,它將通過兩個 arguments:

$this->Controller->listQuestions(1, 'OSPF'); 

您希望call_user_func傳遞一個數組參數:

call_user_func([$this->currentController, $this->currentMethod], $this->params);

由於 PHP 7 你可以這樣稱呼它:

[$this->currentController, $this->currentMethod]($this->params);

早些時候:

    $func = array($this->currentController, $this->currentMethod]);
    $func($this->params);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM