[英]How to load a model in another controller using a custom PHP MVC
我的应用程序具有以下文件夹结构:
- 应用
- 图书馆
- 模型.php
- Controller.php
- View.php
- 楷模
- Settings_Model.php
- Home_Model.php
- 控制器
- settings.php
- home.php
- 意见
- 设定
- index.php
- 家
- index.php
- index.php
- .htaccess
library / Model.php
class Database extends PDO{
public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS){
parent::__construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS);
}
}
库/控制器
class Controller{
function __construct(){
$this->view = new View();
}
public function loadModel($name){
$path = 'application/models/' . $name . '_model.php';
if(file_exists($path)){
require 'application/models/' . $name . '_model.php';
$modelName = $name . '_Model';
$this->model = new $modelName();
}
}
}
图书馆/查看
class View{
function __construct(){
$this->view = new View();
}
public function render($name){
require 'application/views/' . $name . '.php';
}
}
在Models文件夹中,我创建了一个名为Settings_Model.php的 模型 : models / Settings_Model.php
class Settings_Model extends Model{
public function __construct(){parent::__construct();}
function settings(){
$stmt = $this->db->prepare("SELECT * FROM `mv_settings`");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_OBJ);
return $stmt->fetch();
}
}
在Controllers文件夹中,我有一个名为settings.php的 Controller: controllers / settings.php
class Settings extends Controller {
function __construct(){parent::__construct();}
public function index(){
$this->view->settings = $this->model->settings();
$this->view->render('settings/index');
}
}
在Views文件夹中,我创建了一个名为settings的文件夹,在该文件夹中,我创建了一个index.php文件: settings / index
这是如何在settings / index中调用settings方法的方法:
$this->settings->fieldName;
因此,现在转到“ 主页”视图时,我执行了相同的步骤:-创建一个Home_Model类,并在此模型中创建了相同的设置方法:
class Home_Model extends Model{
public function __construct(){parent::__construct();}
function settings(){
$stmt = $this->db->prepare("SELECT * FROM `mv_settings`");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_OBJ);
return $stmt->fetch();
}
}
然后转到controllers文件夹并创建Home controller:
class Home extends Controller {
function __construct(){parent::__construct();}
public function index(){
$this->view->settings = $this->model->settings();
$this->view->render('home/index');
}
}
然后在home / index中调用settings方法:
$this->settings->fieldName;
我要问的是如何避免每次我们需要在另一个视图文件中调用该方法时都不一次又一次地创建相同的方法设置 ?
NB :我还是个新手。
像这样在控制器中尝试
class Settings extends Controller {
function __construct(){
parent::__construct();
$this->load->model('Settings_Model');
}
public function index(){
$this->view->settings = $this->Settings_Model->settings();
$this->view->render('settings/index');
}
}
如果要在所有模型中访问相同的settings()函数,则应在Base model.php中创建它。
class Model{
function settings(){
$stmt = $this->db->prepare("SELECT * FROM `mv_settings`");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_OBJ);
return $stmt->fetch();
}
}
那么您的设置模型需要像
class Settings_Model extends Model{
public function __construct(){parent::__construct();}
}
和你的Home_model
class Home_Model extends Model{
public function __construct(){parent::__construct();}
}
现在,您可以在扩展Model的所有模型中访问settings(),而无需一次又一次地创建它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.