Skip to content

Commit c4a3a2f

Browse files
committed
the third commit.
1 parent 57514c4 commit c4a3a2f

File tree

8 files changed

+122
-0
lines changed

8 files changed

+122
-0
lines changed

connection.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
class Db{
3+
private static $instance = NULL;
4+
5+
private function __construct(){}
6+
7+
private function __clone(){}
8+
9+
public static function getInstance(){
10+
if(!isset(self::$instance)){
11+
$pdo_option[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
12+
self::$instance = new PDO('mysql:host=localhost;dbname=php_mvc','root','root',$pdo_options);
13+
14+
}
15+
return self::instance;
16+
}
17+
}
18+
?>

controlers/pages_controller.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
class PageController{
3+
public function home(){
4+
$first_name = 'Jon';
5+
$last_name = 'Snow';
6+
7+
require_once('views/pages/home.php');
8+
}
9+
10+
public function error(){
11+
require_once('views/pages/error.php');
12+
}
13+
}
14+
?>

controlers/posts_controller.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
class PostsController {
3+
public function index(){
4+
$posts = post::all();
5+
6+
require_once('views/posts/index.php');
7+
8+
}
9+
10+
public function show(){
11+
if(!isset($_GET['id']))
12+
return call('pages', 'error');
13+
14+
15+
$post = Post::find($_GET['id']);
16+
require_once('views/posts/show.php');
17+
}
18+
}
19+
20+
?>

index.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
require_once('connection.php');
3+
4+
if(isset($_GET['controller']) && isset($_GET['action'])){
5+
$controller = $_GET['controller'];
6+
$action = $_GET['action'];
7+
8+
}else{
9+
$controller = 'pages';
10+
$action = 'home';
11+
}
12+
13+
require_once('views/layout.php')
14+
?>

routes.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
function call($controller, $action){
3+
require_once('controllers/' . $controller . '_controller.php');
4+
5+
switch(controller){
6+
case 'page':
7+
$controller = new PagesController();
8+
break;
9+
10+
case 'posts':
11+
require_once('models/post.php');
12+
$controller = new PostsController();
13+
break;
14+
}
15+
16+
17+
18+
$controller->{ $action}();
19+
20+
}
21+
22+
$conrollers = array('pages' => ['home','error'],
23+
'posts' => ['index', 'show']);
24+
25+
if(array_key_exists($controller, $controllers)){
26+
if(in_array($action, $controllers[$controller])){
27+
call($controller, $action);
28+
29+
}else{
30+
call('pages','error');
31+
32+
}
33+
}else{
34+
call('pages','error');
35+
}
36+
37+
?>

views/layout.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head></head>
4+
<body>
5+
<header>
6+
<a href="/php_mvc_blog">Home</a>
7+
8+
</header>
9+
<?php require_once('routes.php'); ?>
10+
11+
<footer>Copyright</footer>
12+
13+
</body>
14+
</html>

views/pages/error.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>Oops, this is the error page. </p>
2+
<p>Looks like somthing wnet wron. </p>

views/pages/home.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p> Hello there <?php echo $first_name . " " . $last_name; ></p>
2+
3+
<p> You successfully landed on the home page. Congrats! </p>

0 commit comments

Comments
 (0)