I'm a beginner in PHP (But I have some Java EE background) I want to create a simple Blog, but I want to do it by using PHP OOP MVC and good/best practices, I'm still thinking how to do things, but I'm really stuck at the idea of how to implement a Controller, and how will it communicate with the views, I find it very different than Java EE, and I don't know where or how to start.
Let me tell you how I'm planning to make it work, at least till the "controller" part :
- Ultimately the design will be :
DB <-> DAO <-> Service <-> Controller <-> View.
But I'll make it simple for the example.
Here we have a DAO class to communicate with the DB.
class UserDAO {
private $db;
// something like this by injecting the database object
public function __construct(Database $db) {
$this->db = $db;
}
public function findUserById($id) {
$req = $db->query("SELECT * FROM user WHERE id = $id");
//.. etc
//.. don't worry about the syntax, what matters is that we're returning the user Object that we found.
return $user;
}
}
The controller will be something like this :
class Controller {
private $dao;
public function __construct(UserDAO $dao) {
$this->dao = $dao;
}
public function loadUser($id) {
return $this->dao->findUserById($id);
}
}
Now, let's say that I have a index.php view file, how can I show the $user infos on the page, or sending data from view to controller later if I want to save a $user, by using best practices, not just like require the views inside controller functions etc..
Thank you very much in advance!
PHPframework to build your blog instead of trying to create your own framework?