Here is a super simple MVC architecture implementation, even a data source with a built-in fixed array, although simple, but in fact many of the PHP framework core implementation of the idea should be similar to this
But some frameworks provide more tools for developers to use, I also want to implement a PHP framework, is currently in the planning, but also want to learn from the framework of the development of more PHP design ideas and methods.
controller.php
Include ' model.php ';
Include ' view.php ';
Class Controller {
Private $model = ';
Private $view = ';
Public Function Controller () {
$this->model = new Model ();
$this->view = new View ();
}
Public Function doaction ($method = ' Defaultmethod ', $params = Array ()) {
if (empty ($method)) {
$this->defaultmethod ();
}else if (method_exists ($this, $method)) {
Call_user_func (Array ($this, $method), $params);
}else{
$this->nonexisting_method ();
}
}
Public Function link_page ($name = ' ") {
$links = $this->model->getlinks ();
$this->view->display ($links);
$result = $this->model->getresult ($name);
$this->view->display ($result);
}
Public Function Defaultmethod () {
$this->br ();
echo "This is the default method.";
}
Public Function Nonexisting_method () {
$this->br ();
echo "This is the Noexisting method.";
}
Public function br () {
echo "<br/>";
}
}
$controller = new Controller ();
$controller->doaction (' Link_page ', ' B ');
$controller->doaction ();
model.php
Code
Class Model {
Private $database = Array (
"A" => "Hello World",
"B" => "OK so Done",
"C" => "Good Bye",
);
//@TODO connect the database
//run the "query and get" result
&nbs p; Public Function GetResult ($name) {
if (empty ($name)) {
return FALSE;
}
if (In_array $name, Array_keys ($this->database)) {
return $ this->database[$name];
}
}
Public Function getlinks () {
$links = "<a href= ' # ' >link a</a> ';
$links. = "<a href= ' >link b</a> ";
$links. = "<a href= ' >link c</a> ";
return $links
}
}
view.php
class View {
Public function display ($output) {
// ob_start ();
echo $output;
}
& nbsp;
}