[英]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.