Skip to content

Commit 8fab753

Browse files
committed
feat: added templates, global middleware and public folder
1 parent b50980a commit 8fab753

29 files changed

+210
-186
lines changed

.htaccess

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<IfModule mod_rewrite.c>
2-
Options -Multiviews
3-
RewriteEngine On
4-
RewriteCond %{REQUEST_FILENAME} !-d
5-
RewriteCond %{REQUEST_FILENAME} !-f
6-
RewriteRule ^(.+)$ index.php?__url__=$1 [QSA,L]
7-
</IfModule>
2+
RewriteEngine On
3+
RewriteCond %{REQUEST_URI} !^/public/
4+
RewriteRule ^(.*)$ public/$1 [L]
5+
</IfModule>

assets/css/app.css

-10
This file was deleted.

composer.json

-13
This file was deleted.

config/config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
define("ENTRY_URL", "http://localhost/php-file-based-routing");
44
define("ASSETS", "http://localhost/php-file-based-routing/assets");
5-
define("ENTRY_FOLDER", "pages");
5+
define("ENTRY_FOLDER", __DIR__ . DIRECTORY_SEPARATOR . "..". DIRECTORY_SEPARATOR ."pages");

config/connection.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
$host = "localhost:3306";
44
$dbname = "dbname";
5-
$user = "dbusername";
6-
$pass = "userpass";
5+
$user = "root";
6+
$pass = "";
77

88
try {
99
$conn = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $user, $pass);

index.php

-3
This file was deleted.

pages/+middleware.global.php

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="middleware" >[Global Middleware: /]</div>

pages/404.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<link rel="stylesheet" type="text/css" href="<?php echo ASSETS; ?>/app.css">
7-
<script type="text/javascript" src="<?php echo ASSETS; ?>/app.js" ></script>
4+
<?php useTemplate("head") ?>
85
</head>
96
<body>
107
<div style="padding: 5rem 0rem;" >

pages/auth/+middleware.global.php

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="middleware" >[Global Middleware: /auth]</div>

pages/auth/index.get.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<link rel="stylesheet" type="text/css" href="<?php echo ASSETS; ?>/app.css">
7-
<script type="text/javascript" src="<?php echo ASSETS; ?>/app.js" ></script>
4+
<?php useTemplate("head") ?>
85
</head>
96
<body>
107
<div>Showing data behind middleware</div>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="middleware" >[Global Middleware: /auth/nested]</div>

pages/auth/nested/+middleware.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
<?php
2-
$is_not_found = true;
3-
?>
2+
throw404();

pages/auth/nested/index.get.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<link rel="stylesheet" type="text/css" href="<?php echo ASSETS; ?>/app.css">
7-
<script type="text/javascript" src="<?php echo ASSETS; ?>/app.js" ></script>
4+
<?php useTemplate("head") ?>
85
</head>
96
<body>
107
<div>Showing data behind scoped middleware</div>

pages/index.get.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<script type="text/javascript" src="<?php echo ASSETS; ?>/js/app.js" ></script>
7-
<script src="https://cdn.tailwindcss.com"></script>
4+
<?php useTemplate("head") ?>
85
</head>
96
<body class="flex items-center justify-center h-screen w-screen" >
107
<section class="mx-auto p-12 border rounded-md space-y-6" >
@@ -26,6 +23,8 @@
2623
</div>
2724
</div>
2825

26+
<hr>
27+
2928
<div>
3029
<p>POST REQUEST</p>
3130
<form method="POST" action="post" >

pages/post/[id]/edit.get.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<link rel="stylesheet" type="text/css" href="<?php echo ASSETS; ?>/app.css">
7-
<script type="text/javascript" src="<?php echo ASSETS; ?>/app.js" ></script>
4+
<?php useTemplate("head") ?>
85
</head>
96
<body>
107
<div>Editing #<?php echo $_GET['id']; ?> post</div>

pages/post/[id]/index.delete.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<link rel="stylesheet" type="text/css" href="<?php echo ASSETS; ?>/app.css">
7-
<script type="text/javascript" src="<?php echo ASSETS; ?>/app.js" ></script>
4+
<?php useTemplate("head") ?>
85
</head>
96
<body>
107
<div>Deleting #<?php echo $_GET['id']; ?> post</div>

pages/post/[id]/index.get.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<link rel="stylesheet" type="text/css" href="<?php echo ASSETS; ?>/app.css">
7-
<script type="text/javascript" src="<?php echo ASSETS; ?>/app.js" ></script>
4+
<?php useTemplate("head") ?>
85
</head>
96
<body>
107
<div>Showing #<?php echo $_GET['id']; ?> post</div>

pages/post/index.get.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<link rel="stylesheet" type="text/css" href="<?php echo ASSETS; ?>/app.css">
7-
<script type="text/javascript" src="<?php echo ASSETS; ?>/app.js" ></script>
4+
<?php useTemplate("head") ?>
85
</head>
96
<body>
107
<div>Showing all posts...</div>

pages/post/index.post.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<link rel="stylesheet" type="text/css" href="<?php echo ASSETS; ?>/app.css">
7-
<script type="text/javascript" src="<?php echo ASSETS; ?>/app.js" ></script>
4+
<?php useTemplate("head") ?>
85
</head>
96
<body>
107
<div>Publishing Post...</div>

pages/user/[id].get.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<link rel="stylesheet" type="text/css" href="<?php echo ASSETS; ?>/app.css">
7-
<script type="text/javascript" src="<?php echo ASSETS; ?>/app.js" ></script>
4+
<?php useTemplate("head") ?>
85
</head>
96
<body>
107
<div>Showing user <span style="font-weight: bold;" ><?php echo $_GET['id']; ?></span></div>

pages/your-awesome-page.get.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<link rel="stylesheet" type="text/css" href="<?php echo ASSETS; ?>/app.css">
7-
<script type="text/javascript" src="<?php echo ASSETS; ?>/app.js" ></script>
4+
<?php useTemplate("head") ?>
85
</head>
96
<body>
107
<div>This is your awesome page</div>
11-
<a href="index">Go to index</a>
8+
<a href="./">Go to index</a>
129
</body>
1310
</html>

public/.htaccess

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<IfModule mod_rewrite.c>
2+
Options -Multiviews
3+
RewriteEngine On
4+
RewriteCond %{REQUEST_FILENAME} !-d
5+
RewriteCond %{REQUEST_FILENAME} !-f
6+
RewriteRule ^(.+)$ index.php?__url__=$1 [QSA,L]
7+
</IfModule>

public/assets/css/app.css

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
body {
2+
background: #eee;
3+
font-size: 1.4rem;
4+
padding: 2rem;
5+
max-width: 500px;
6+
margin: 0 auto;
7+
color: #111;
8+
text-align: center;
9+
font-family: "Open sans", "arial";
10+
}
11+
12+
.middleware {
13+
font-size: small;
14+
color: #2ecc71;
15+
border: 1px solid #2ecc71;
16+
padding: 1px solid gray;
17+
padding: 0.5rem;
18+
margin-bottom: 0.5rem;
19+
}
File renamed without changes.

public/index.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Require the entry of the router, which is used for finding the correct page in the `pages` folder
4+
* Also requireing the helpers, which contains functions like load templates and throw404
5+
*/
6+
require ".." . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "config.php";
7+
require ".." . DIRECTORY_SEPARATOR . "router" . DIRECTORY_SEPARATOR . "helpers.php";
8+
require ".." . DIRECTORY_SEPARATOR . "router" . DIRECTORY_SEPARATOR . "bootstrap.php";
9+
10+
/**
11+
* If your projects needs to use composer, you can include it here if you want it global
12+
* https://getcomposer.org/
13+
*/
14+
// require "vendor/autoload.php";
15+
16+
/**
17+
* Some configurations and database connections if you needed some
18+
*/
19+
// require "config/connection.php";

public/robots.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disallow: *

0 commit comments

Comments
 (0)