Skip to content

Instantly share code, notes, and snippets.

View sonha's full-sized avatar

Simon sonha

  • Ha Noi
View GitHub Profile
@sonha
sonha / layout.php
Created August 9, 2016 07:34
layout
<DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<a href='/mvc'>Home</a>
<a href='?controller=posts&action=index'>Posts</a>
</header>
@sonha
sonha / call_demo.php
Created August 9, 2016 07:34
call_demo
<?php
$post = new Post('SonHA', 'Simple MVC Tutorial');
?>
@sonha
sonha / post.php
Created August 9, 2016 07:33
Model Post
<?php
class Post {
// we define 3 attributes
// they are public so that we can access them using $post->author directly
public $id;
public $author;
public $content;
public function __construct($id, $author, $content) {
$this->id = $id;
@sonha
sonha / PostController.php
Created August 9, 2016 07:32
PostController
<?php
class PostsController {
public function index() {
// we store all the posts in a variable
$posts = Post::all();
require_once('views/posts/index.php');
}
public function show() {
// we expect a url of form ?controller=posts&action=show&id=x
@sonha
sonha / routes_v1.php
Created August 9, 2016 07:32
routes_v1
<?php
function call($controller, $action) {
require_once('controllers/' . $controller . '_controller.php');
switch($controller) {
case 'pages':
$controller = new PagesController();
break;
case 'posts':
// we need the model to query the database later in the controller
@sonha
sonha / error.php
Created August 9, 2016 07:21
error view
<p>Oops, this is the error page.</p>
<p>Looks like something went wrong.</p>
@sonha
sonha / home.php
Created August 9, 2016 07:20
home
<p>Hello there <?php echo $first_name . ' ' . $last_name; ?>!<p>
<p>You successfully landed on the home page. Congrats!</p>
@sonha
sonha / PageController.php
Created August 9, 2016 07:18
PageController
<?php
class PagesController {
public function home() {
$first_name = 'Son';
$last_name = 'Ha';
require_once('views/pages/home.php');
}
public function error() {
require_once('views/pages/error.php');
@sonha
sonha / routes.php
Created August 9, 2016 07:17
routes
<?php
function call($controller, $action) {
// require the file that matches the controller name
require_once('controllers/' . $controller . '_controller.php');
// create a new instance of the needed controller
switch($controller) {
case 'pages':
$controller = new PagesController();
break;
@sonha
sonha / layout.php
Created August 9, 2016 07:16
layout
<DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<a href='/mvc'>Home</a>
</header>
<?php require_once('routes.php'); ?>