This project implements a simple PHP router for basic URL routing within a web application. It allows developers to define routes (URLs) and associate them with specific actions or content, represented by callback functions or included PHP files. The router handles clean URLs, provides a basic 404 error page, and injects dynamic content into a main layout. It's designed for small to medium-sized projects where a lightweight routing solution is needed.
# Simple PHP Router
This is a basic PHP router designed to handle simple URL routing within a web application. It allows you to define routes and associate them with specific actions or content.
## Overview
This router provides a clean and straightforward way to map URLs to specific PHP files or functions. It's ideal for small to medium-sized projects where a full-fledged framework might be overkill.
## Features
* **Simple Route Definition:** Easily define routes using the `addRoute()` method.
* **Callback Functions:** Associate each route with a callback function that handles the request.
* **404 Error Handling:** Gracefully handles requests to undefined routes with a custom 404 error page.
* **Clean URL Handling:** Removes leading and trailing slashes from URLs for consistent matching.
* **Content Injection:** The router injects the content in the index.php file.
## File Structure
* **`index.php`:** The main entry point of the application. It includes the router and defines the basic HTML structure.
* **`router.php`:** Contains the `Router` class, which handles route management and execution.
* **`routes.php`:** Defines the application's routes and their associated callbacks.
* **`home.php`:** Example content file for the `/home` route.
* **`about.php`:** Example content file for the `/about` route.
* **`contact.php`:** Example content file for the `/contact` route.
## Usage
### Defining Routes (`routes.php`)
1. **Include `Router.php`:**
```php
require_once 'Router.php';
```
2. **Create a Router Instance:**
```php
$router = new Router();
```
3. **Add Routes:** Use the `addRoute()` method to define routes and their callbacks:
```php
$router->addRoute('/', function() {
$content = "This page is index!";
});
$router->addRoute('home', function() {
require_once 'home.php';
});
$router->addRoute('about', function() {
require_once 'about.php';
});
$router->addRoute('contact', function() {
require_once 'contact.php';
});
```
* The first argument to `addRoute()` is the URL path (e.g., `/`, `home`, `about`).
* The second argument is a callback function that will be executed when the route is matched.
* The callback function can set the `$content` variable or include a php file.
### Running the Router (`index.php`)
1. **Include `router.php` and `routes.php`:**
```php
require_once 'router.php';
require_once 'routes.php';
```
2. **Run the Router:**
```php
$router->run();
```
This will process the current URL and execute the appropriate callback.
3. **Content injection:**
```php
<div id="content">
<?php
if (!isset($content)) {
$content = '';
}
echo $content;
?>
</div>
```
This code will inject the content of the `$content` variable in the page.
### Creating Content Files (`home.php`, `about.php`, `contact.php`)
These files contain the content that will be displayed for their respective routes.
Example (`contact.php`):
```php
$content = 'This page is CONTACT';
```
## Limitations
*Basic Functionality: This is a very basic router and does not include advanced features like route parameters, middleware, or complex routing patterns.
*No HTTP Method Handling: It doesn't differentiate between HTTP methods (GET, POST, etc.).
*No error handling: The code does not have a good error handling.
# License
This project is open-source and available under the MIT License.
|