|
| 1 | +# PHP Route |
| 2 | + |
| 3 | +**PHP Route** is a lightweight and flexible routing library for PHP applications. It provides an easy-to-use system for defining routes, grouping, middlewares, error handling, maintenance mode, and custom not-found responses. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +* Define routes with HTTP methods: GET, POST, PUT, PATCH, DELETE, OPTIONS. |
| 10 | +* Support for `any()` method to handle all HTTP methods. |
| 11 | +* Route grouping with optional prefix and middlewares. |
| 12 | +* Global and route-specific middlewares. |
| 13 | +* Custom error handling and maintenance mode. |
| 14 | +* Custom 404 Not Found and maintenance responses. |
| 15 | +* PSR-7 compatible Request and Response objects using Guzzle. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +Install via Composer: |
| 22 | + |
| 23 | +```bash |
| 24 | +composer require refkinscallv/php-route |
| 25 | +``` |
| 26 | + |
| 27 | +Include Composer autoloader in your project: |
| 28 | + |
| 29 | +```php |
| 30 | +require __DIR__ . '/vendor/autoload.php'; |
| 31 | +``` |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Basic Usage |
| 36 | + |
| 37 | +```php |
| 38 | +use RFRoute\Route\Route; |
| 39 | +use RFRoute\Http\Request; |
| 40 | +use RFRoute\Http\Response; |
| 41 | + |
| 42 | +// Simple GET route |
| 43 | +Route::get('/hello', function(Request $req, Response $res) { |
| 44 | + $res->send("Hello World!"); |
| 45 | +}); |
| 46 | + |
| 47 | +// POST route |
| 48 | +Route::post('/submit', function(Request $req, Response $res) { |
| 49 | + $data = $req->getParsedBody(); |
| 50 | + $res->json(['status' => 'success', 'data' => $data]); |
| 51 | +}); |
| 52 | + |
| 53 | +// Route with a controller |
| 54 | +Route::get('/user', [App\Controllers\UserController::class, 'index']); |
| 55 | +``` |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## Route Groups and Middlewares |
| 60 | + |
| 61 | +```php |
| 62 | +Route::group('/admin', function() { |
| 63 | + Route::get('/dashboard', function(Request $req, Response $res) { |
| 64 | + $res->send("Admin Dashboard"); |
| 65 | + }); |
| 66 | +}, [ |
| 67 | + function(Request $req, Response $res) { |
| 68 | + if (!isset($_SESSION['admin'])) { |
| 69 | + $res->send("Unauthorized", 401); |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | +]); |
| 74 | + |
| 75 | +// Global middleware |
| 76 | +Route::middleware([ |
| 77 | + function(Request $req, Response $res) { |
| 78 | + error_log($req->getUri()); |
| 79 | + } |
| 80 | +], function() { |
| 81 | + // Routes inside this callback will have the global middleware applied |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## Error Handling |
| 88 | + |
| 89 | +```php |
| 90 | +Route::setErrorHandler(function(Throwable $e, Request $req, Response $res) { |
| 91 | + $res->send("Something went wrong: " . $e->getMessage(), 500); |
| 92 | +}); |
| 93 | + |
| 94 | +Route::setNotFoundHandler(function(Request $req, Response $res) { |
| 95 | + $res->send("Custom 404 Page Not Found", 404); |
| 96 | +}); |
| 97 | + |
| 98 | +Route::setMaintenanceHandler(function(Request $req, Response $res) { |
| 99 | + $res->send("Service Unavailable: Maintenance Mode", 503); |
| 100 | +}); |
| 101 | + |
| 102 | +// Enable maintenance mode |
| 103 | +Route::enableMaintenance(true); |
| 104 | +``` |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +## Dispatching Routes |
| 109 | + |
| 110 | +At the end of your script or router file: |
| 111 | + |
| 112 | +```php |
| 113 | +Route::dispatch($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); |
| 114 | +``` |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +## Response Helpers |
| 119 | + |
| 120 | +```php |
| 121 | +use RFRoute\Http\Response; |
| 122 | + |
| 123 | +$res = new Response(); |
| 124 | + |
| 125 | +// Send plain text |
| 126 | +$res->send("Hello World", 200); |
| 127 | + |
| 128 | +// Send JSON |
| 129 | +$res->json(['status' => 'ok', 'message' => 'Success']); |
| 130 | + |
| 131 | +// Redirect |
| 132 | +$res->redirect('/home'); |
| 133 | +``` |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +## PSR-7 Request Object |
| 138 | + |
| 139 | +```php |
| 140 | +use RFRoute\Http\Request; |
| 141 | + |
| 142 | +$req = new Request(); |
| 143 | +$method = $req->getMethod(); |
| 144 | +$uri = $req->getUri(); |
| 145 | +$body = $req->getParsedBody(); |
| 146 | +$query = $req->getQueryParams(); |
| 147 | +``` |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +## Contributing |
| 152 | + |
| 153 | +Contributions are welcome! You can help by: |
| 154 | + |
| 155 | +* Reporting bugs |
| 156 | +* Suggesting features |
| 157 | +* Submitting pull requests |
| 158 | + |
| 159 | +Please follow these steps: |
| 160 | + |
| 161 | +1. Fork the repository |
| 162 | +2. Create a new branch (`git checkout -b feature-name`) |
| 163 | +3. Make your changes and commit (`git commit -am 'Add new feature'`) |
| 164 | +4. Push to the branch (`git push origin feature-name`) |
| 165 | +5. Open a Pull Request |
| 166 | + |
| 167 | +--- |
| 168 | + |
| 169 | +## Reporting Issues |
| 170 | + |
| 171 | +If you encounter a bug or have a feature request, please open an issue on the [GitHub repository](https://github.com/refkinscallv/php-route/issues). Include: |
| 172 | + |
| 173 | +* A clear description of the problem |
| 174 | +* Steps to reproduce the issue |
| 175 | +* Any relevant error messages or stack traces |
| 176 | + |
| 177 | +--- |
| 178 | + |
| 179 | +## License |
| 180 | + |
| 181 | +This library is licensed under the [MIT License](LICENSE). |
0 commit comments