Skip to content

Commit 815fc7f

Browse files
authored
Merge pull request #2 from Sheane-mario/main
directory structure
2 parents 6edc0e5 + bff9b78 commit 815fc7f

14 files changed

+129
-24
lines changed

app/controllers/Home.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
home controller
1+
<?php
2+
3+
class Home extends Controller
4+
{
5+
6+
public function index()
7+
{
8+
echo "this is the home controller";
9+
$this->view('home');
10+
}
11+
}

app/controllers/Products.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
class Products extends Controller
4+
{
5+
6+
public function index()
7+
{
8+
echo "this is the products controller ";
9+
$this->view('products');
10+
}
11+
}

app/controllers/_404.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
class _404 extends Controller
4+
{
5+
6+
public function index()
7+
{
8+
echo "404 page not found controller";
9+
}
10+
}

app/core/App.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
4+
class App
5+
{
6+
private $controller = 'Home';
7+
private $method = 'index';
8+
9+
private function splitURL()
10+
{
11+
$URL = $_GET['url'] ?? 'home';
12+
$URL = explode('/', $URL);
13+
return $URL;
14+
}
15+
public function loadController()
16+
{
17+
$URL = $this->splitURL();
18+
19+
$filename = "../app/controllers/" . ucfirst($URL[0]) . ".php";
20+
if (file_exists($filename)) {
21+
require $filename;
22+
$this->controller = ucfirst($URL[0]);
23+
} else {
24+
$filename = "../app/controllers/_404.php";
25+
require $filename;
26+
$this->controller = '_404';
27+
}
28+
$controller = new $this->controller;
29+
call_user_func_array([$controller, $this->method], []);
30+
}
31+
}

app/core/Controller.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
class Controller
4+
{
5+
6+
public function view($name)
7+
{
8+
$filename = "../app/views/" . $name . ".view.php";
9+
if (file_exists($filename)) {
10+
require $filename;
11+
} else {
12+
$filename = "../app/views/404.view.php";
13+
require $filename;
14+
}
15+
}
16+
}

app/core/Database.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
$string = "mysql:host=" . DBHOST . ";dbname=" . DBNAME;
4+
$con = new PDO($string, DBUSER, DBPASS);
5+
6+
show($con);

app/core/Model.php

Whitespace-only changes.

app/core/config.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
if ($_SERVER['SERVER_NAME'] == 'localhost') {
4+
/** database config **/
5+
define('DBNAME', 'group_project_demo');
6+
define('DBHOST', 'localhost');
7+
define('DBUSER', 'root');
8+
define('DBPASS', 'mario#123');
9+
define('ROOT', 'http://localhost/MVC-tutorial/group-project-demo/public');
10+
} else {
11+
/** database config **/
12+
define('DBNAME', 'group_project_demo');
13+
define('DBHOST', 'localhost');
14+
define('DBUSER', 'root');
15+
define('DBPASS', '');
16+
define('ROOT', 'http://group-project-demo.herokuapp.com');
17+
}

app/core/functions.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
function show($stuff)
4+
{
5+
echo "<pre>";
6+
print_r($stuff);
7+
echo "</pre>";
8+
}

app/core/init.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
require 'config.php';
4+
require 'functions.php';
5+
require 'Database.php';
6+
require 'Model.php';
7+
require 'Controller.php';
8+
require 'App.php';

app/views/404.view.php

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>view file not found</h1>

app/views/home.view.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h1>Home page view</h1>
2+
3+
<!-- <link rel="stylesheet" href="<?= ROOT ?>/assets/css/customer-singup.css"> -->
4+
<!-- <img src="<?= ROOT ?>/assets/images/image1.jpg" alt=""> -->

app/views/products.view.php

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Products page view</h1>

public/index.php

+5-23
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
11
<?php
22

3-
function show($stuff)
4-
{
5-
echo "<pre>";
6-
print_r($stuff);
7-
echo "</pre>";
8-
}
9-
function splitURL()
10-
{
11-
$URL = $_GET['url'] ?? 'home';
12-
$URL = explode('/', $URL);
13-
return $URL;
14-
}
15-
function loadController()
16-
{
17-
$URL = splitURL();
3+
session_start();
184

19-
$filename = "../app/controllers/" . ucfirst($URL[0]) . ".php";
20-
if (file_exists($filename)) {
21-
require $filename;
22-
} else {
23-
echo "controller not found";
24-
}
25-
}
26-
loadController();
5+
require "../app/core/init.php";
6+
7+
$app = new App();
8+
$app->loadController();

0 commit comments

Comments
 (0)