Skip to content

Commit c98b41d

Browse files
committed
update README.md
1 parent 2c133e6 commit c98b41d

9 files changed

+206
-44
lines changed

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/material_theme_project_new.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php-router.iml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php.xml

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/phpunit.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+127-44
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# php-router
2-
This router is designed to help you easily and efficiently handle HTTP requests and responses in your PHP applications. It is inspired by the popular Laravel framework, and aims to provide a similar experience and functionality.
2+
3+
This router is designed to help you easily and efficiently handle HTTP requests and responses in your PHP applications.
4+
It is inspired by the popular Laravel framework, and aims to provide a similar experience and functionality.
35

46
# Getting Started
57

@@ -16,77 +18,151 @@ This router is designed to help you easily and efficiently handle HTTP requests
1618
- [TODO](#todo)
1719

1820
## Installation
21+
1922
You may use composer to intall [MrF0o/php-router](https://github.com/MrF0o/php-router) by running this command:
23+
2024
```bash
2125
composer require mrf0o/php-router
2226
```
2327

28+
## Setup
29+
After installing the package you may run your app either via the php command line or via a reverse-proxy server such as Apache.
30+
31+
#### Method 1: PHP CLI
32+
create an `index.php` for example in the root of your project (you can name whatever you want) with this content:
33+
34+
```php
35+
<?php
36+
37+
include_once "vendor/autoload.php";
38+
39+
use Mrfoo\PHPRouter\Router;
40+
41+
Router::get('/', function() {
42+
echo 'Hello World!';
43+
});
44+
45+
Router::run();
46+
```
47+
48+
```shell
49+
php -S localhost:8888 index.php
50+
```
51+
This command will run your app on port 8888. And if everything went correctly, visiting http://localhost:8888 on your browser should show the text 'Hello World'.
52+
53+
#### Method 2: Reverse proxy
54+
Here you can find a .htaccess file example that you can use with the rewrite rules needed for this router to run correctly. You can use the same example code from the previous method and make sure to place the file somewhere under the document root Apache expects (Generally htdocs).
55+
56+
> Apache by default uses index.php as the main file so it will serve it by default. but if you want to use this router globally withing you project you want all traffic to point to the index.php even tho the user tried to access a different folder withing you project.
57+
58+
```apacheconf
59+
<IfModule mod_rewrite.c>
60+
<IfModule mod_negotiation.c>
61+
Options -MultiViews
62+
</IfModule>
63+
64+
RewriteEngine On
65+
66+
RewriteCond %{REQUEST_FILENAME} -d [OR]
67+
RewriteCond %{REQUEST_FILENAME} -f
68+
RewriteRule ^ ^$1 [N]
69+
70+
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
71+
RewriteRule ^(.*)$ public/$1
72+
73+
RewriteCond %{REQUEST_FILENAME} !-d
74+
RewriteCond %{REQUEST_FILENAME} !-f
75+
RewriteRule ^ index.php
76+
</IfModule>
77+
```
78+
79+
Note: you need to change index.php to the file that contains the Router::run() call.
80+
81+
> [!IMPORTANT]
82+
> This router does not support subfolders yet. You may need extra configurations in your .htaccess to make it work.
83+
2484
## The Router class
25-
After the installation is complete you can start using the router by including The `Router` class. for example this route will fire whenever `/hello` is hit:
85+
86+
[After the installation is complete you can start using the router by including The `Router` class. for example this
87+
route will fire whenever `/hello` is hit:]()
88+
2689
```php
2790
use Mrfoo\PHPRouter\Router;
2891

2992
Router::get('/hello', function () {
30-
echo '<h1>hello there</h1>';
93+
echo '<h1>hello there</h1>';
3194
});
3295

3396
Router::run();
3497
```
3598

36-
You can see that each request is represented as a static method of the Router class, thus you can change the `get` with each of these methods: `post`, `put`, `patch` and `delete`.
99+
You can see that each request is represented as a static method of the Router class, thus you can change the `get` with
100+
each of these methods: `post`, `put`, `patch` and `delete`.
37101

38102
Make sure to run the Router using the `run` static method.
39103

40104
## Route parameters
41-
To capture a segment in your url you can use route parameters, these parameters will be passed to the handler function in order.
105+
106+
To capture a segment in your url you can use route parameters, these parameters will be passed to the handler function
107+
in order.
42108

43109
```php
44110
Router::get('/hello/{name}', function ($name) {
45-
echo '<h1>hello '.$name.'</h1>';
111+
echo '<h1>hello '.$name.'</h1>';
46112
})
47113
```
48114

49-
> optional parameters aren't implemented yet, so this route will be matched only if the parameter {name} is present, otherwise it will give a 404 error
115+
> optional parameters aren't implemented yet, so this route will be matched only if the parameter {name} is present,
116+
> otherwise it will give a 404 error
50117
51118
## Regular Expression Constraints
52-
Sometimes, you may need to constrain a parameter using regular expression, you can do this using the `where` method on the Route instance.
119+
120+
Sometimes, you may need to constrain a parameter using regular expression, you can do this using the `where` method on
121+
the Route instance.
122+
53123
```php
54124
Router::get('/user/{name}', function ($name) {
55-
// ...
125+
// ...
56126
})->where('name', '[A-Za-z]+');
57127

58128
Router::get('/user/{id}/{name}', function (string $id, string $name) {
59-
// ...
129+
// ...
60130
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
61131
```
62132

63133
## Named Routes
64-
You may give names to your routes using the `name` method on Route instance, this will make it easier to reference your routes elsewhere in your code using the `route` helper method later.
134+
135+
You may give names to your routes using the `name` method on Route instance, this will make it easier to reference your
136+
routes elsewhere in your code using the `route` helper method later.
65137

66138
```php
67139
Router::get('/user/profile', function () {
68-
// ...
140+
// ...
69141
})->name('profile');
70142
```
71143

72144
## Grouping routes
73-
Also, you can create route groups using the `group` method of the router class, each Route registered in the callback will share the properties passed to the `group` method.
145+
146+
Also, you can create route groups using the `group` method of the router class, each Route registered in the callback
147+
will share the properties passed to the `group` method.
74148

75149
```php
76150
Router::group(['prefix' => '/user'], function () {
77-
// here all routes will be prefixed with /user
151+
// here all routes will be prefixed with /user
78152
Router::get('/update', fn () => die('not implemented')); /* /user/update */
79153
});
80154
```
81155

82156
## Route redirects
157+
83158
You can redirect a route to another route using the `redirect` method on the Router class.
84159

85160
```php
86161
Router::redirect('/old', '/new');
87162
```
88163

89-
by default the redirect will be a `302` redirect, but you can change that by passing the status code as the third argument.
164+
by default the redirect will be a `302` redirect, but you can change that by passing the status code as the third
165+
argument.
90166

91167
```php
92168
Router::redirect('/old', '/new', 301);
@@ -99,19 +175,23 @@ Router::permanentRedirect('/old', '/new');
99175
```
100176

101177
## Generating URLs
102-
You can generate URLs for your routes using the `route` helper method, this method accepts the name of the route and a variable count of parameters to be passed to the route.
178+
179+
You can generate URLs for your routes using the `route` helper method, this method accepts the name of the route and a
180+
variable count of parameters to be passed to the route.
103181

104182
```php
105183
Router::get('/user/{id}', function ($id) {
106-
// ...
184+
// ...
107185
})->name('user.profile');
108186

109187
$url = route('user.profile', 1);
110188
// $url = 'http://example.com/user/1'
111189
```
112190

113191
# Middlewares
114-
Middlewares are a great way to filter requests before they reach your route handler, in this router library Middlewares are represented as classes that Overrides the `handle` method in the \Mrfoo\PHPRouter\Middleware class.
192+
193+
Middlewares are a great way to filter requests before they reach your route handler, in this router library Middlewares
194+
are represented as classes that Overrides the `handle` method in the \Mrfoo\PHPRouter\Middleware class.
115195

116196
```php
117197
<?php
@@ -121,32 +201,33 @@ use Mrfoo\PHPRouter\Core\Middleware;
121201

122202
class AuthMiddleware extends Middleware
123203
{
124-
public function handle()
125-
{
126-
if (!isset($_SESSION['user_id'])) {
127-
// redirect to login page
128-
header('Location: /login');
129-
exit;
130-
}
131-
}
204+
public function handle()
205+
{
206+
if (!isset($_SESSION['user_id'])) {
207+
// redirect to login page
208+
header('Location: /login');
209+
exit;
210+
}
211+
}
132212
}
133213
```
134214

135-
the handle method will be called before the route handler, so you can do any checks you want and redirect the user if needed.
215+
the handle method will be called before the route handler, so you can do any checks you want and redirect the user if
216+
needed.
136217

137218
Then you can use the middleware in your routes like this:
138219

139220
```php
140221
Router::get('/user/profile', function () {
141-
// ...
222+
// ...
142223
})->name('user.profile')->middleware(AuthMiddleware::class);
143224
```
144225

145226
and you may assign multiple middlewares to a route like this:
146227

147228
```php
148229
Router::get('/user/profile', function () {
149-
// ...
230+
// ...
150231
})->name('user.profile')->middleware([AuthMiddleware::class, AnotherMiddleware::class]);
151232
```
152233

@@ -160,23 +241,24 @@ use Mrfoo\PHPRouter\Core\Middleware;
160241

161242
class AuthMiddleware extends Middleware
162243
{
163-
public function handle()
164-
{
165-
if (!isset($_SESSION['user_id'])) {
166-
// redirect to login page
167-
header('Location: /login');
168-
exit;
169-
}
170-
}
171-
172-
public function terminate()
173-
{
174-
// do some cleanup
175-
}
244+
public function handle()
245+
{
246+
if (!isset($_SESSION['user_id'])) {
247+
// redirect to login page
248+
header('Location: /login');
249+
exit;
250+
}
251+
}
252+
253+
public function terminate()
254+
{
255+
// do some cleanup
256+
}
176257
}
177258
```
178259

179260
## Quick Example
261+
180262
```php
181263
<?php
182264
include './vendor/autoload.php';
@@ -198,10 +280,11 @@ Router::patch('/user/profile', [UserController::class, 'update'])->name('user.pr
198280
Router::run();
199281
```
200282

201-
Here I used `UserController` class as an example to demonstrate, the other convention to use route handlers besides the callback function.
202-
283+
Here I used `UserController` class as an example to demonstrate, the other convention to use route handlers besides the
284+
callback function.
203285

204286
# TODO
287+
205288
- [X] Route Grouping
206289
- [X] Route redirects
207290
- [X] `route` helper function, this should be globally available

0 commit comments

Comments
 (0)