Skip to content

Commit 733d033

Browse files
base upload
1 parent c482d97 commit 733d033

File tree

9 files changed

+182
-0
lines changed

9 files changed

+182
-0
lines changed

.htaccess

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Options -Indexes
2+
RedirectMatch 404 /\.git
3+
4+
<IfModule mod_rewrite.c>
5+
RewriteEngine on
6+
7+
# RewriteCond %{SERVER_PORT} 80
8+
# RewriteCond %{HTTPS} off
9+
# RewriteCond %{HTTP:X-Forwarded-Proto} !https
10+
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
11+
12+
RewriteCond %{REQUEST_FILENAME} !-f
13+
RewriteCond %{REQUEST_FILENAME} !-d
14+
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
15+
</IfModule>

assets/router.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var preURL = "";
2+
var curURL = "";
3+
4+
window.setInterval(function () {
5+
curURL = document.location.href;
6+
7+
if (curURL != preURL) {
8+
9+
$(".pageAjax").attr("style", "opacity: .6;");
10+
var split = curURL.split('https://keiranchippendaleuk-urban-parakeet-r5wvwpxxp7p356p-8000.preview.app.github.dev/');
11+
if (split[1] == "/" || split[1] == "") {
12+
document.location.href = "home";
13+
}
14+
$.ajax({
15+
url: `pages/Router.php?page=${split[1]}`,
16+
method: 'GET',
17+
success: function (data) {
18+
if (data) {
19+
$('.pageAjax').html(data);
20+
$(".pageAjax").attr("style", "opacity: 1;");
21+
} else {
22+
window.history.pushState('data', 'simple php router', 'home');
23+
console.log('Page Not found: ' + data);
24+
}
25+
}
26+
});
27+
28+
}
29+
30+
preURL = curURL;
31+
}, 500);
32+
33+
$(document).ready(function () {
34+
$(document).on("click", "a[target='page']", function (pageOpen) {
35+
if ($(this).attr('target') == "modal" || $(this).attr('href') == "javaScript:void();") {
36+
37+
} else {
38+
if ($(this).attr('href') !== "#") {
39+
curURL = "";
40+
preURL = "";
41+
pageOpen.preventDefault();
42+
window.history.pushState('data', 'simplae php router', $(this).attr("href"));
43+
}
44+
}
45+
});
46+
});

index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
include('pages/template.php');
3+
?>

pages/Router.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
$pageURL = isset($_GET['page']) ? $_GET['page'] : null;
3+
$pageLink = basename(explode('?', $pageURL)[0]);
4+
5+
if (strpos($pageURL, '?') !== false) {
6+
$pageParams = explode('?', $pageURL)[1];
7+
$params = array();
8+
parse_str($pageParams, $params);
9+
10+
foreach ($params as $key => $value) {
11+
$params[$key] = filter_var($value, FILTER_SANITIZE_STRING);
12+
}
13+
}
14+
15+
$pageFile = "{$pageLink}.php";
16+
17+
if (file_exists($pageFile)) {
18+
require_once($pageFile);
19+
} else {
20+
echo "Page not found";
21+
}
22+
?>

pages/home.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>This is a simple page router made by <a href="https://djkeiran.co.uk">djkeiran</a></h1>

pages/page2.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Page2</h1>

pages/page3.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Page3</h1>

pages/template.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
7+
<title>Simple php page router</title>
8+
<link
9+
rel="stylesheet"
10+
href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"
11+
/>
12+
<script
13+
type="text/javascript"
14+
src="https://code.jquery.com/jquery-1.12.4.min.js"
15+
></script>
16+
<script
17+
type="text/javascript"
18+
src="https://code.jquery.com/ui/1.13.0/jquery-ui.min.js"
19+
></script>
20+
<script
21+
type="text/javascript"
22+
src="https://omnipotent.net/jquery.sparkline/2.1.2/jquery.sparkline.min.js"
23+
></script>
24+
<style>
25+
body {
26+
background-color: #343a40;
27+
color: #f8f9fa;
28+
}
29+
.card {
30+
background-color: #42464d;
31+
border-color: #343a40;
32+
color: #f8f9fa;
33+
}
34+
.btn-primary {
35+
background-color: #007bff;
36+
border-color: #007bff;
37+
}
38+
.btn-primary:hover {
39+
background-color: #0069d9;
40+
border-color: #0062cc;
41+
}
42+
</style>
43+
</head>
44+
<body>
45+
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
46+
<div class="container">
47+
<a class="navbar-brand" href="/home" target="page">Simple PHP page router</a>
48+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
49+
<span class="navbar-toggler-icon"></span>
50+
</button>
51+
<div class="collapse navbar-collapse" id="navbarNav">
52+
<ul class="navbar-nav ms-auto">
53+
<li class="nav-item">
54+
<a class="nav-link" href="/home" target="page">Home</a>
55+
</li>
56+
<li class="nav-item">
57+
<a class="nav-link" href="/page2" target="page">page2</a>
58+
</li>
59+
<li class="nav-item">
60+
<a class="nav-link" href="/page3" target="page">page3</a>
61+
</li>
62+
</ul>
63+
</div>
64+
</div>
65+
</nav>
66+
<div class="container">
67+
<div class="row">
68+
<div class="col-md-6 offset-md-3 mt-5">
69+
<div class="page">
70+
<div class="pageAjax">
71+
72+
</div>
73+
</div>
74+
</div>
75+
</div>
76+
</div>
77+
<script
78+
src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"
79+
></script>
80+
<script src="assets/router.js"></script>
81+
</body>
82+
</html>

readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Simple page router in php
2+
please read the license
3+
4+
This is a basic page router in ajax and window.pushstate made in php and js
5+
6+
# Notes:
7+
Look in assets/router.js and change the domain
8+
and you can add your own pages in the pages folder feel free to fork this and use it this is open source
9+
if you want to give credits
10+
11+
enjoy the page router :)

0 commit comments

Comments
 (0)