This file contains hidden or 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
| <DOCTYPE html> | |
| <html> | |
| <head> | |
| </head> | |
| <body> | |
| <header> | |
| <a href='/mvc'>Home</a> | |
| <a href='?controller=posts&action=index'>Posts</a> | |
| </header> |
This file contains hidden or 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 | |
| $post = new Post('SonHA', 'Simple MVC Tutorial'); | |
| ?> |
This file contains hidden or 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 | |
| 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; |
This file contains hidden or 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 | |
| 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 |
This file contains hidden or 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 | |
| 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 |
This file contains hidden or 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
| <p>Oops, this is the error page.</p> | |
| <p>Looks like something went wrong.</p> |
This file contains hidden or 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
| <p>Hello there <?php echo $first_name . ' ' . $last_name; ?>!<p> | |
| <p>You successfully landed on the home page. Congrats!</p> |
This file contains hidden or 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 | |
| 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'); |
This file contains hidden or 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 | |
| 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; |
This file contains hidden or 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
| <DOCTYPE html> | |
| <html> | |
| <head> | |
| </head> | |
| <body> | |
| <header> | |
| <a href='/mvc'>Home</a> | |
| </header> | |
| <?php require_once('routes.php'); ?> |