Skip to content

Commit 46cecec

Browse files
committed
MVC framework
0 parents  commit 46cecec

21 files changed

+618
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

.htaccess

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<IfModule mod_rewrite.c>
2+
RewriteEngine on
3+
RewriteRule ^$ public/ [L]
4+
RewriteRule (.*) public/$1 [L]
5+
</IfModule>
6+

libraries/Controller.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
class Controller
6+
{
7+
8+
public function model($model)
9+
{
10+
require_once "src/App/Models" .$model. ".php";
11+
}
12+
13+
14+
public function view($view)
15+
{
16+
17+
18+
require_once "views/" .$view. ".php";
19+
}
20+
}

libraries/Database.php

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

public/.htaccess

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RewriteEngine On
2+
RewriteCond %{REQUEST_FILENAME} !-f
3+
RewriteCond %{REQUEST_FILENAME} !-d
4+
RewriteCond %{REQUEST_FILENAME} !-l
5+
RewriteRule ^ index.php

public/index.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
include ('../src/App/config/config.php');
4+
//use App\Controllers;
5+
//use Framework;
6+
7+
$url_path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
8+
9+
10+
spl_autoload_register(function (string $class_name) {
11+
12+
require ROOT . "/src/" . str_replace("\\", "/", $class_name) . ".php";
13+
14+
});
15+
16+
$router = new Framework\Router;
17+
18+
19+
20+
$router->add("/{controller}", '#^/(?<controller>[a-z]+)\/$#iu');
21+
$router->add("/{controller}/{action}", '#^/(?<controller>[a-zA-Z]+)\/(?<action>[a-zA-Z]+)/$#iu');
22+
23+
$router->add("/{controller}/{id}", '#^/(?<controller>[a-zA-Z]+)\/(?<id>[0-9]+)\/$#iu');
24+
$router->add("/{controller}/{action}/{id}", '#^/(?<controller>[a-zA-Z]+)\/(?<action>[a-zA-Z]+)\/(?<id>[0-9]+)/$#iu');
25+
26+
$router->add("/{controller}/{id}/{slug}", '#^/(?<controller>[a-zA-Z]+)\/(?<id>[0-9]+)\/(?<slug>[a-zA-Z]+)/$#iu');
27+
28+
$router->add("/{controller}/{action}/{id}/{slug}", '#^/(?<controller>[a-zA-Z]+)\/(?<action>[a-zA-Z]+)\/(?<id>[0-9]+)\/(?<slug>[a-zA-Z]+)/$#iu');
29+
30+
31+
echo $router->matchResult;
32+
33+
$router->dispatch($url_path);
34+
35+
36+

public/styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
html {
2+
font-family: sans-serif;
3+
margin: 1em;
4+
}

src/App/Controllers/Home.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
class Home
6+
{
7+
public function index()
8+
{
9+
require_once ('../views/home_index.php');
10+
}
11+
}

src/App/Controllers/IdController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
class IdController
6+
{
7+
public function index($id)
8+
{
9+
$id1 = $id;
10+
require_once ('../views/id_index.php');
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
class IdSlugController
6+
{
7+
public function index($id, $slug)
8+
{
9+
10+
$id1 = $id;
11+
$slug1 = $slug;
12+
require_once ('../views/id_slug_index.php');
13+
}
14+
}

src/App/Controllers/Products.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use App\Models\Product;
6+
7+
class Products
8+
{
9+
public function index()
10+
{
11+
// $model = new Product;
12+
//
13+
// $products = $model->getData();
14+
15+
require_once('../views/products_index.php');
16+
}
17+
18+
public function show()
19+
{
20+
require_once('../views/products_show.php');
21+
}
22+
}

src/App/Controllers/TestClass.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
class TestClass
6+
{
7+
public function index()
8+
{
9+
$a = ['Title 111', 'Title 222', 'Title 333'];
10+
require_once('../views/test.php');
11+
}
12+
}

src/App/Models/Product.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use PDO;
6+
7+
class Product
8+
{
9+
public function getData(): array
10+
{
11+
$dsn = "mysql:host=localhost;dbname=product_db;charset=utf8;port=3306";
12+
13+
$pdo = new PDO($dsn, "product_db_user", "secret", [
14+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
15+
]);
16+
17+
$stmt = $pdo->query("SELECT * FROM product");
18+
19+
return $stmt->fetchAll(PDO::FETCH_ASSOC);
20+
}
21+
}

src/App/config/config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
define( 'BASE_URL', 'http://phpmvc.localhost/' );
4+
define( 'ROOT', dirname ( dirname( dirname( __DIR__ ) ) ) );
5+
define( 'APPROOT', dirname( __DIR__ ) );
6+

0 commit comments

Comments
 (0)