【发布时间】:2017-05-10 10:20:21
【问题描述】:
不确定是否可能,但我想做的是通过获取请求将查询字符串传递给我的控制器。从这里我使用 $_GET['name'] 获取查询字符串值。然后我想做的是使用该 GET 值并将其附加为我将传递给我的模型以返回我需要的数据的方法的名称。
这是因为多个操作将不同的查询字符串值传递给同一个控制器,然后使用该值从我的模型中的不同函数获取数据。
例如
控制器
class ControllerName extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('my_model');
}
public function index() {
$queryName = $_GET['query_string']
// e.g. $queryName = 'customer'
//Use the query string and append to method to pass to Model function
$result = $this->my_model->$queryName._get_query(); //???
//e.g. $result = $this->my_model->customer_get_query();
}
}
我的模型
class My_model extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function customer_get_query() {
//...run database query and return result
}
}
有什么想法吗?
【问题讨论】:
-
你可以试试 $this->my_model->{$queryName.'_get_query'}();
-
非常感谢,辛苦了!
标签: php function codeigniter methods controllers