Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6460df4

Browse files
Add views folder structure, database class, config variables
1 parent e7a1ce6 commit 6460df4

File tree

12 files changed

+214
-9
lines changed

12 files changed

+214
-9
lines changed

‎app/bootstrap.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
2-
// Load libraries
3-
require_once 'libraries/Core.php';
4-
require_once 'libraries/Controller.php';
5-
require_once 'libraries/Database.php';
2+
// Load Config
3+
require_once 'config/config.php';
4+
5+
// Autoload Core Libraries
6+
spl_autoload_register(function($className)
7+
{
8+
require_once "libraries/{$className}.php";
9+
});

‎app/config/config.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
// DB Params
3+
define('DB_HOST', 'localhost');
4+
define('DB_USER', 'root');
5+
define('DB_PASS', '');
6+
define('DB_NAME', 'test');
7+
// App Root
8+
define('APPROOT', dirname(dirname(__FILE__)));
9+
// URL Root
10+
define('URLROOT', 'http://localhost/php-basic-mvc');
11+
// Site Name
12+
define('SITENAME', 'PHP Basic MVC');

‎app/controllers/Pages.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
<?php
2-
class Pages
2+
class Pagesextends Controller
33
{
44
public function __construct()
55
{
6-
6+
// !!! Only for testing the connection to the database
7+
$this->postModel = $this->model('Post');
78
}
89

910
public function index()
1011
{
11-
12+
$posts = $this->postModel->getPosts();
13+
14+
$data = [
15+
'title' => 'Welcome',
16+
'posts' => $posts
17+
];
18+
19+
$this->view('pages/index', $data);
1220
}
1321

14-
public function about($id)
22+
public function about()
1523
{
16-
echo $id;
24+
$data = [
25+
'title' => 'About'
26+
];
27+
$this->view('pages/about', $data);
1728
}
1829
}

‎app/libraries/Controller.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Base Controller
4+
* Loads the models and views
5+
*/
6+
class Controller
7+
{
8+
// Load model
9+
public function model($model)
10+
{
11+
// Require model file
12+
require_once '../app/models/' . $model . '.php';
13+
14+
// Instantiate model
15+
return new $model;
16+
}
17+
18+
// Load view
19+
public function view($view, $data = [])
20+
{
21+
// Check for view file
22+
if (file_exists('../app/views/' . $view . '.php'))
23+
{
24+
require_once '../app/views/' . $view . '.php';
25+
}
26+
else
27+
{
28+
// View does not exist
29+
die('View does not exist');
30+
}
31+
}
32+
}

‎app/libraries/Database.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* PDO Database Class
4+
* Connect to database
5+
* Create prepared statements
6+
* Bind values
7+
* Return rows and results
8+
*/
9+
class Database
10+
{
11+
private $host = DB_HOST;
12+
private $user = DB_USER;
13+
private $pass = DB_PASS;
14+
private $name = DB_NAME;
15+
16+
private $db_handler;
17+
private $stmt;
18+
private $error;
19+
20+
public function __construct()
21+
{
22+
// Set DSN
23+
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->name;
24+
$options = [
25+
PDO::ATTR_PERSISTENT => true,
26+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
27+
];
28+
29+
// Create PDO Instance
30+
try
31+
{
32+
$this->db_handler = new PDO($dsn, $this->user, $this->pass, $options);
33+
}
34+
catch(PDOException $e)
35+
{
36+
$this->error = $e->getMessage();
37+
echo $this->error;
38+
}
39+
}
40+
41+
// Prepare statement with query
42+
public function query($sql)
43+
{
44+
$this->stmt = $this->db_handler->prepare($sql);
45+
}
46+
47+
// Bind values
48+
public function bind($param, $value, $type = null)
49+
{
50+
if (is_null($type))
51+
{
52+
switch (true)
53+
{
54+
case is_int($value):
55+
$type = PDO::PARAM_INT;
56+
break;
57+
58+
case is_bool($value):
59+
$type = PDO::PARAM_BOOL;
60+
break;
61+
62+
case is_null($value):
63+
$type = PDO::PARAM_NULL;
64+
break;
65+
66+
default:
67+
$type = PDO::PARAM_STR;
68+
}
69+
}
70+
71+
$this->stmt->bindValue($param, $value, $type);
72+
}
73+
74+
// Execute the prepared statement
75+
public function execute()
76+
{
77+
return $this->stmt->execute();
78+
}
79+
80+
// Get result set as array of objects
81+
public function resultSet()
82+
{
83+
$this->execute();
84+
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
85+
}
86+
87+
// Get single record as object
88+
public function single()
89+
{
90+
$this->execute();
91+
return $this->stmt->fetch(PDO::FETCH_OBJ);
92+
}
93+
94+
// Get row count
95+
public function rowCount()
96+
{
97+
return $this->stmt->rowCount();
98+
}
99+
}

‎app/models/Post.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
class Post
3+
{
4+
private $db;
5+
6+
public function __construct()
7+
{
8+
$this->db = new Database;
9+
}
10+
11+
public function getPosts()
12+
{
13+
$this->db->query('SELECT * FROM posts');
14+
15+
return $this->db->resultSet();
16+
}
17+
}

‎app/views/inc/footer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script src="<?= URLROOT; ?>/js/main.js"></script>
2+
</body>
3+
</html>

‎app/views/inc/header.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<link rel="stylesheet" href="<?= URLROOT; ?>/css/style.css">
6+
<title><?= SITENAME; ?></title>
7+
</head>
8+
<body>

‎app/views/pages/about.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php require APPROOT . '/views/inc/header.php'; ?>
2+
3+
<h1> <?= $data['title']; ?> </h1>
4+
5+
<?php require APPROOT . '/views/inc/footer.php'; ?>

‎app/views/pages/index.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php require APPROOT . '/views/inc/header.php'; ?>
2+
3+
<h1> <?= $data['title']; ?> </h1>
4+
<ul>
5+
<?php foreach($data['posts'] as $post) : ?>
6+
<li><?= $post->title; ?></li>
7+
<?php endforeach; ?>
8+
</ul>
9+
10+
<?php require APPROOT . '/views/inc/footer.php'; ?>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /