Skip to content

Commit 87d2b06

Browse files
committed
Initial commit
0 parents commit 87d2b06

File tree

13 files changed

+297
-0
lines changed

13 files changed

+297
-0
lines changed

.htaccess

Lines changed: 7 additions & 0 deletions
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>

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## PHP File-based routing
2+
This is my unstable version of file-based routing, inspired by Javascript Libraries/Frameworks, to make my project's URL tiny-little-bit-beautiful even my backend is an instant legacy code.
3+
4+
### Sooo @frozeeen, again what's this?
5+
Well, here you go
6+
```python
7+
https://yourawesome.web/post?id=ABCDEFG12345
8+
to
9+
https://yourawesome.web/post/ABCDEFG12345
10+
```
11+
12+
### Installation
13+
1. Simply clone this project into your machine.
14+
2. Update the config inside the `index.php`.
15+
3. and presto! all set.
16+
17+
### Structure
18+
The structure is very simple
19+
```python
20+
pages # This is where your pages will live
21+
assets # Maybe some of your CSS, JS and other assets
22+
index.php # And this is where some tiny-little-bit magic polynomial time happens, it's the router
23+
.htaccess # We don't talk about .htaccess -Dani
24+
```
25+
26+
### How to use this?
27+
To use this, let's head into `pages` and make some files.
28+
29+
#### Static routing
30+
```python
31+
Path: pages/your-awesome-page.php
32+
URL: http://website.com/your-awesome-page
33+
```
34+
35+
#### Dynamic routing
36+
To create a dynamic url like `https://website.com/post?id=YOURPOSTID` we're going to do it like this.
37+
```python
38+
# Create a file
39+
Path: pages/post/[id].php
40+
URL: https://website.com/post/YOURPOSTID
41+
```
42+
and to access the value in our code, it's just like normal `$_GET`, the name between the brackets `[]` is the parameter name (they call this `slug`).
43+
```php
44+
Showing post ID: <?php echo $_GET['id']; ?>
45+
```
46+
47+
#### Nexted Dynamic routing
48+
We can also create a folder to become our slug.
49+
```python
50+
# Create folder an file
51+
Path: pages/post/[id]/edit.php
52+
URL: pages/post/YOURPOSTID/edit
53+
```
54+
So the file structure will look like this, and let's add additional files.
55+
```
56+
pages/
57+
--post/
58+
----[id]/
59+
------edit.php
60+
------show.php
61+
------delete.php
62+
```

assets/app.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}

assets/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Wop wop");

index.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
#|==================================
3+
#| Initialize global settings
4+
#|==================================
5+
define("ASSETS", "http://localhost/php-file-based-routing/assets");
6+
7+
#|==================================
8+
#| Initialize the query string
9+
#|==================================
10+
$REQUEST_URI = $_SERVER['QUERY_STRING'] == '' ? ['index'] : explode('/', $_SERVER['REQUEST_URI']);
11+
$REQUEST_DEPTH = count($REQUEST_URI);
12+
$REQUEST_COUNTED = 0;
13+
$ENTRY_FOLDER = "pages";
14+
15+
#|==================================
16+
#| Find the page in the folder
17+
#|==================================
18+
$destination_path = $ENTRY_FOLDER;
19+
$current_type = 0; // 0 Folder | 1 File
20+
$is_not_found = false;
21+
for($r = ($_SERVER['QUERY_STRING'] == '') ? 0 : 2; $r < $REQUEST_DEPTH; $r++){
22+
$part = $REQUEST_URI[$r];
23+
if( $part == '' ) continue;
24+
25+
$REQUEST_COUNTED++;
26+
$moving_path = ($destination_path . '/' . $part);
27+
28+
# Check if a raw file exist
29+
$with_extension = str_contains($moving_path, '.') ? $moving_path : $moving_path . ".php";
30+
if( file_exists($with_extension) ){
31+
$destination_path = $with_extension;
32+
$current_type = 1;
33+
$is_not_found = $r != $REQUEST_DEPTH - 1;
34+
break;
35+
36+
# Move inside the folder
37+
}else if( is_dir($moving_path) == 1 ){
38+
$destination_path = $moving_path;
39+
$current_type = 0;
40+
41+
}else{
42+
43+
# Find the `[slug]`
44+
$is_slug_found = false;
45+
$is_slugged = false;
46+
$scanned_dir = scandir($destination_path);
47+
for($i = 2, $x = count($scanned_dir); $i < $x; $i++){
48+
if( str_contains($scanned_dir[$i], '[') ){
49+
50+
# Slug folder
51+
if( str_contains($scanned_dir[$i], '.') == false ){
52+
$_GET[substr($scanned_dir[$i], 1, -1)] = $part;
53+
$destination_path = $destination_path . '/' . $scanned_dir[$i];
54+
$current_type = 0;
55+
56+
# Slug file
57+
}else if( $r == $REQUEST_DEPTH - 1 ){
58+
$file_name = explode('.', $scanned_dir[$i])[0];
59+
$_GET[substr($file_name, 1, -1)] = $part;
60+
$destination_path = $destination_path . '/' . $scanned_dir[$i];
61+
$current_type = 1;
62+
$is_slugged = true;
63+
}
64+
65+
$is_slug_found = true;
66+
break;
67+
}
68+
}
69+
70+
$is_not_found = !$is_slug_found;
71+
if( $is_slugged || $is_not_found ){
72+
break;
73+
}
74+
}
75+
76+
}
77+
78+
#|==================================
79+
#| Check if the last iteration is already a file
80+
#|==================================
81+
if( $current_type == 0 && file_exists($destination_path . "/index.php") ){
82+
$destination_path .= "/index.php";
83+
}else if( $current_type == 0 && $is_not_found == false ){
84+
$is_not_found = true;
85+
}
86+
87+
#|==================================
88+
#|
89+
#| REQUIRE your scripts here before render
90+
#|
91+
#|==================================
92+
# require_once ".backend/your_script.php";
93+
94+
#|==================================
95+
#| Load the selected page
96+
#|==================================
97+
if( file_exists($destination_path) && $is_not_found == false ){
98+
require_once $destination_path;
99+
}else{
100+
require_once $ENTRY_FOLDER . "/404.php";
101+
}
102+
103+
#|==================================
104+
#|
105+
#| REQUIRE your scripts here after render
106+
#|
107+
#|==================================
108+
# require_once ".backend/your_script.php";
109+
110+
?>

pages/404.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>
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>
8+
</head>
9+
<body>
10+
<div style="padding: 5rem 0rem;" >
11+
<h3 style="color: gray; font-weight: normal; margin-bottom: 0.5rem;" ><span style="font-weight: bold;" >404</span> | Are you lost hoooman?</h3>
12+
</div>
13+
</body>
14+
</html>

pages/index.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<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>
8+
</head>
9+
<body>
10+
<h3 style="margin-bottom: 0.5rem;" >PHP File-based router</h3>
11+
<div>Now go, build something</div>
12+
<small>
13+
<a href="your-awesome-page">Awesome page</a> |
14+
<a href="post">Post</a> |
15+
<a href="post/ABCDEFG">Show post</a> |
16+
<a href="user/@foobar">Show user</a>
17+
</small>
18+
</body>
19+
</html>

pages/post/[id]/delete.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<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>
8+
</head>
9+
<body>
10+
<div>Deleting #<?php echo $_GET['id']; ?> post</div>
11+
</body>
12+
</html>

pages/post/[id]/edit.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<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>
8+
</head>
9+
<body>
10+
<div>Editing #<?php echo $_GET['id']; ?> post</div>
11+
</body>
12+
</html>

pages/post/[id]/index.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<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>
8+
</head>
9+
<body>
10+
<div>Showing #<?php echo $_GET['id']; ?> post</div>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)