Skip to content

A dyamic SPA page routing system In php feel free to contribute or check out or live demo at the link below

License

Notifications You must be signed in to change notification settings

keiranchippendale/PHP-Page-Routing-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP-Page-Routing-System

A small PHP routing system that maps clean URLs to PHP files.
Built to work well with SPA-style front ends without using a framework.

This project exists because sometimes you just want:

  • clean URLs
  • predictable file-based routing
  • full control over what runs
  • no dependencies

How it works

All requests are routed through index.php using .htaccess.

Instead of loading full pages, the router returns HTML inside a JSON response.
The front end swaps the content without reloading the page.

Example flow:

/test1
→ index.php?page=test1
→ api/router.php
→ pages/test1.php
→ { html: "<h1>Test Page One</h1>" }

The layout and navigation never reload — only the page content changes.


Project structure

/
├── index.php
├── .htaccess
├── api/
│   └── router.php
└── pages/
    ├── home.php
    ├── test1.php
    └── test2.php
  • index.php
    App shell and front-end logic

  • .htaccess
    Handles clean URLs and routes everything through PHP

  • api/router.php
    Resolves routes, loads page files, and returns HTML as JSON

  • pages/
    Plain PHP files that output HTML only (no layout)


Routing rules

  • /home
  • /homepages/home.php
  • /test1pages/test1.php
  • /test2pages/test2.php
  • Unknown routes return a themed 404 response

Routes are normalised and restricted to prevent directory traversal or invalid paths.


Adding a new page

  1. Create a file in pages/
pages/about.php
  1. Visit it in the browser
/about

That’s it. No config, no registration step.


Error handling

If a page does not exist or the route is invalid, the router returns:

  • a JSON error response
  • a themed HTML error view
  • a safe status code inside the payload

The error UI is designed to sit inside the app layout, not replace it.


Front end

The default front end uses:

  • Tailwind CSS
  • Alpine.js
  • Flowbite
  • Font Awesome

You can replace the front end entirely — the router does not depend on it.


Why this exists

Frameworks are great, but sometimes they’re too much.

This project is for:

  • internal tools
  • small dashboards
  • admin panels
  • experiments
  • learning how routing actually works

If you can read basic PHP, you can understand the whole system.


License

https://www.wtfpl.net/about/

Legacy Version (archived Discontinued)

https://github.com/keiranchippendale/simple-php-router-old

About

A dyamic SPA page routing system In php feel free to contribute or check out or live demo at the link below

Topics

Resources

License

Stars

Watchers

Forks