My application have this folders stucture:
- application
- library
- Model.php
- Controller.php
- View.php
- models
- Settings_Model.php
- Home_Model.php
- controllers
- settings.php
- home.php
- views
- settings
- index.php
- home
- 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);
}
}
library/Controller
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();
}
}
}
library/View
class View{
function __construct(){
$this->view = new View();
}
public function render($name){
require 'application/views/' . $name . '.php';
}
}
In Models folder I created this model called 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();
}
}
In Controllers folder I have this Controller called settings.php : 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');
}
}
And In Views folder I created a folder called settings and inside this folder I created an index.php file: settings/index
and this is how to call the settings method inside settings/index:
$this->settings->fieldName;
So now when we go to Home view, I did the same procedure: - create a Home_Model class and inside this model I created the same settings method:
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();
}
}
and then goes to the controllers folder and create Home controller:
class Home extends Controller {
function __construct(){parent::__construct();}
public function index(){
$this->view->settings = $this->model->settings();
$this->view->render('home/index');
}
}
and then call the settings method inside home/index:
$this->settings->fieldName;
What Im asking is how to avoid not to create the same method settings again and again each time we need to call that method in another view file???
NB: Im still a newbie.
Try like this in your controller
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');
}
}
if you want to access the same settings() function in all models then you should create it in 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();
}
}
then your settings model need to be like
class Settings_Model extends Model{
public function __construct(){parent::__construct();}
}
and your Home_model
class Home_Model extends Model{
public function __construct(){parent::__construct();}
}
Now you can access settings() in all your models extending Model, no need to create it again and again.