Skip to content

Instantly share code, notes, and snippets.

@mdzzohrabi
Created December 11, 2015 13:39
Show Gist options
  • Save mdzzohrabi/e83aa6945da48a1fcb7f to your computer and use it in GitHub Desktop.
Save mdzzohrabi/e83aa6945da48a1fcb7f to your computer and use it in GitHub Desktop.
PHP Simple MVC
<?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