Search by

dayal / php-page-router

Dayal574

A lightweight PHP file-based page router with configurable section directories.

Package info

github.com/Dayal574/php-page-router

pkg:composer/dayal/php-page-router

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-12-01 00:06 UTC

This package is auto-updated.

Last update: 2026-12-01 00:13:35 UTC


README

A small Composer package for file-based PHP routing. It maps request paths to PHP files inside a page/ directory and supports configurable section folders such as admin, agent, and affiliate.

Requirements

  • PHP 8.1+
  • Composer
  • A web server configured to send application routes to the project's front controller (index.php).

Install

After publishing this package to Packagist:

composer require dayal/php-page-router

Until it is published to Packagist, install it from a local path repository or a VCS repository. See Composer's repositories documentation.

Project structure

your-project/
├── index.php
├── page/
│   ├── index.php
│   ├── about.php
│   ├── 404.php
│   ├── admin/
│   │   ├── index.php
│   │   ├── dashboard.php
│   │   └── 404.php
│   ├── agent/
│   │   ├── index.php
│   │   └── profile.php
│   └── affiliate/
│       ├── index.php
│       └── reports.php
└── vendor/

Usage

In the project's root index.php:

<?php

require __DIR__ . '/vendor/autoload.php';

use Dayal\PageRouter\Router;

$router = new Router(['admin', 'agent', 'affiliate']);
$router->dispatch();

By default, the package assumes the front controller is in the project root and pages are in <project-root>/page.

If your pages directory is somewhere else, pass its path as the second constructor argument:

$router = new Router(
    ['admin', 'agent', 'affiliate'],
    __DIR__ . '/resources/pages'
);

$router->dispatch();

URL mapping

URL File
/ page/index.php
/about page/about.php
/admin page/admin/index.php
/admin/dashboard page/admin/dashboard.php
/agent page/agent/index.php
/agent/profile page/agent/profile.php
/affiliate/reports page/affiliate/reports.php
/unknown page/404.php
/admin/unknown page/admin/404.php, otherwise page/404.php

Only the section names passed to the constructor are treated as section roots. For example, if admin is not in the array, /admin/dashboard maps to page/admin/dashboard.php as an ordinary nested path.

Web server requirement

The web server must route non-existent paths to index.php.

Nginx

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

Apache

Enable mod_rewrite and add this .htaccess in the project root:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Set the web document root to the project directory for this .htaccess example. For better production security, prefer setting the document root to a dedicated public/ directory and placing the front controller there.

Important security notes

  • The router validates URL path segments and rejects . / .. traversal segments.
  • It resolves the target using realpath() and verifies that the resolved PHP file remains inside the intended page directory.
  • This is a page router, not an authentication system. Protect admin/agent/affiliate pages with your own session-based authentication or middleware before including protected content.
  • Do not place secrets, .env, uploads, backups, or private files inside a web-accessible document root.
  • Use HTTPS in production.
  • The router includes PHP files directly. Those files execute with the router's include scope; use explicit application bootstrap files for shared dependencies.

Development note

This package scaffold is ready for local Composer path/VCS testing. To install it globally with composer require dayal/php-page-router, you must publish it under a Packagist package name you control, or configure your own Composer repository. The name in composer.json is a placeholder vendor name; change dayal to your Packagist/GitHub vendor namespace before publishing.