PHP MVC 传递多个参数
PHP MVC pass multiple parameters
由于我缺乏 PHP 编程经验,我发现很难理解如何在 URL 中传递多个参数。
我有以下 URL http://some.domain/examr/pages/listQuestions/1/OSPF
其中 objective 是将最后 2 个参数传递给 listQuestions
控制器函数,但是,只有一个被传递. URL 改写为 $_GET['URL']
.
我的框架有一个文件 core.php 文件,它包含核心 class 用于基本上将 URL 拆分成一个数组(或者我应该说爆炸)并加载方法和控制器的属性。
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
函数,我可以看到它正在获取所有 URL 参数,到目前为止我觉得还不错 :-)
此外,我还有另一个文件,其中包含用于加载模型和视图的基本控制器。
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);
}
}
}
这一切都集中在一个 bootstrap 文件中,该文件加载了核心和基础控制器,该文件加载在 index.php 文件中并实例化了 class 核心。
从前面的 URL 你可以看到我有一个名为 pages 的控制器,它正在扩展基本控制器并且想将最后 2 URL 参数传递给函数 listQuestions
(即 w.i.p)。
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= [QSA,L]
</IfModule>
感谢任何帮助。
call_user_func_array
将数组作为参数,然后 将其提取 为单独的参数。所以在这种情况下它会传递两个参数:
$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);
由于我缺乏 PHP 编程经验,我发现很难理解如何在 URL 中传递多个参数。
我有以下 URL http://some.domain/examr/pages/listQuestions/1/OSPF
其中 objective 是将最后 2 个参数传递给 listQuestions
控制器函数,但是,只有一个被传递. URL 改写为 $_GET['URL']
.
我的框架有一个文件 core.php 文件,它包含核心 class 用于基本上将 URL 拆分成一个数组(或者我应该说爆炸)并加载方法和控制器的属性。
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
函数,我可以看到它正在获取所有 URL 参数,到目前为止我觉得还不错 :-)
此外,我还有另一个文件,其中包含用于加载模型和视图的基本控制器。
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);
}
}
}
这一切都集中在一个 bootstrap 文件中,该文件加载了核心和基础控制器,该文件加载在 index.php 文件中并实例化了 class 核心。
从前面的 URL 你可以看到我有一个名为 pages 的控制器,它正在扩展基本控制器并且想将最后 2 URL 参数传递给函数 listQuestions
(即 w.i.p)。
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= [QSA,L]
</IfModule>
感谢任何帮助。
call_user_func_array
将数组作为参数,然后 将其提取 为单独的参数。所以在这种情况下它会传递两个参数:
$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);