Skip to content

Commit 72ef208

Browse files
committed
1.0.0
0 parents  commit 72ef208

File tree

13 files changed

+1098
-0
lines changed

13 files changed

+1098
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/vendor/
2+
node_modules/
3+
npm-debug.log
4+
yarn-error.log
5+
6+
# Laravel 4 specific
7+
bootstrap/compiled.php
8+
app/storage/
9+
10+
# Laravel 5 & Lumen specific
11+
public/storage
12+
public/hot
13+
14+
# Laravel 5 & Lumen specific with changed public path
15+
public_html/storage
16+
public_html/hot
17+
18+
storage/*.key
19+
.env
20+
Homestead.yaml
21+
Homestead.json
22+
/.vagrant
23+
.phpunit.result.cache
24+
25+
/public/build
26+
/storage/pail
27+
.env.backup
28+
.env.production
29+
.phpactor.json
30+
auth.json

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Refkinscallv
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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).

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "refkinscallv/php-route",
3+
"description": "PHP Routing Library provides a flexible and easy-to-use routing system for PHP applications",
4+
"version": "1.0.0",
5+
"type": "library",
6+
"license": "MIT",
7+
"autoload": {
8+
"psr-4": {
9+
"RFRoute\\": "src/"
10+
}
11+
},
12+
"authors": [
13+
{
14+
"name": "Refkinscallv",
15+
"email": "refkinscallv@gmail.com"
16+
}
17+
],
18+
"minimum-stability": "stable",
19+
"require": {
20+
"psr/http-message": "^2.0",
21+
"guzzlehttp/psr7": "^2.8"
22+
}
23+
}

0 commit comments

Comments
 (0)