Created
December 11, 2015 13:39
-
-
Save mdzzohrabi/e83aa6945da48a1fcb7f to your computer and use it in GitHub Desktop.
PHP Simple MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Controller | |
*/ | |
abstract class Controller { | |
/** | |
* Render view | |
* @param string $view View file | |
* @param array $params View parameters | |
* @return string Rendered view | |
*/ | |
protected function render( $view , array $params = array() ) { | |
explode( $params ); | |
return include $view; | |
} | |
} | |
/** | |
* Model | |
*/ | |
abstract class Model { | |
} | |
/** | |
* Home controller | |
*/ | |
class HomeController extends Controller { | |
public function indexAction() { | |
// Find book with id 2 | |
$book = Book::findById( 2 ); | |
// Render view | |
return $this->render('index.html.php',array( | |
'bookId' => $book->getId(), | |
'bookName'=> $book->getName() | |
)); | |
} | |
} | |
/** | |
* Book model | |
* Interact with database | |
*/ | |
class Book extends Model { | |
// Book id | |
protected $id; | |
// Book name | |
protected $name; | |
public function getId() { | |
return $this->id; | |
} | |
public function getName() { | |
return $this->name; | |
} | |
} | |
?> | |
// index.html.php | |
<html> | |
<head></head> | |
<body> | |
<h1><?=$bookName;?></h1> | |
Book id : <?=$bookId;?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment