简体   繁体   中英

PHP MVC pass multiple parameters

Due to my lack of PHP programming experience, I am finding it hard to understand how I can pass multiple parameters in 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. The URL is rewritten into $_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;
    }
}

}

I have used the print_r function on the $this->params and I can see it is picking up all the URL parameters, so far I guess so good:-)

In addition I have another file that holds the base controller which is used to load the models and the views.

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);
        
     }
 }
 
 }

This all comes together in a bootstrap file that loads both the core and base controller, which is loaded in an index.php file and instantiates the class core.

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);
}

I can see from the print_r($params); that only 1 param is being passed and I have no idea where my mistake is or how can I troubleshoot this further.

I have also the following rewrite rule

<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>

Any help is appreciated.

call_user_func_array takes an array as an argument and extracts it into individual arguments. So in this case it would pass two arguments:

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

You want call_user_func to pass a single argument which is an array:

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

Since PHP 7 you can call it like this:

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

Earlier:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM