I'm trying to build a simple mvc framework to understand. I've heard advice about rolling you own framework but I'd rather learn by playing with some basic examples since diving into using pre existing frameworks without understanding everything seems daunting to me. So, I have a controller, model and view. In my database file I'm able to retrieve data using $result[1]->title as an example, but I'd like to have that information passed to the view (which is basically just my template file). I'm not really sure how to do this, I've tried a couple of methods and googled, but I've got conflicting answers. I'd appreciate it if anyone could point me in the right direction and maybe give advice for best practices for mvc structure since I got a lot of conflicting answers on that. I've attached some of the code I'm using below. Controller.php
<?php
class Controller {
private $model;
private $load;
private $name;
public function __construct($model, $load) {
$this->model = $model;
$this->load = $load;
}
public function index() {
$name = 'view';
$this->load->view($name);
$this->model->index();
}
}
Database.php
<?php
class Database {
function getData() {
$username = 'username';
$password = '****';
try {
$dbh = new PDO('mysql:host=localhost;dbname=db_name', $username, $password);
} catch (PDOException $e) {
echo $e->getMessage();
}
//PDO Class
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
$sth = $dbh->query("SELECT * FROM posts");
$result = $sth->fetchAll(PDO::FETCH_OBJ);
//print_r($result);
//echo $result[1]->title;
}
}
Model.php
<?php
class Model {
public function __construct(){
$this->string = "MVC + PHP = Awesome!";
}
public function index() {
}
}