|
1 | 1 | # InitPHP Router |
2 | 2 |
|
3 | | -This is an open source library that lets you create and manage advanced routes for HTTP requests. |
| 3 | +A fast, framework-agnostic HTTP router for PHP. Define expressive routes for any |
| 4 | +HTTP method, group them by prefix, domain, port or client IP, attach middleware, |
| 5 | +inject dependencies into your handlers, generate URLs from named routes and |
| 6 | +serve static files — all on top of any [PSR-7](https://www.php-fig.org/psr/psr-7/) |
| 7 | +request/response implementation. |
4 | 8 |
|
5 | | -[](https://packagist.org/packages/initphp/router) [](https://packagist.org/packages/initphp/router) [](https://packagist.org/packages/initphp/router) [](https://packagist.org/packages/initphp/router) [](https://packagist.org/packages/initphp/router) |
| 9 | +[](https://packagist.org/packages/initphp/router) [](https://packagist.org/packages/initphp/router) [](https://packagist.org/packages/initphp/router) [](https://packagist.org/packages/initphp/router) |
6 | 10 |
|
7 | 11 | ## Features |
8 | 12 |
|
9 | | -- Full support for GET, POST, PUT, DELETE, OPTIONS, PATCH, HEAD and ANY request methods. |
10 | | -- Variable request methods with (Laravel-like) `$_REQUEST['_method']`. (Default is off, optionally can be activated) |
11 | | -- Controller support. (HomeController@about or HomeController::about) |
12 | | -- Middleware/Filter (before and after) support. |
13 | | -- Static and dynamic route patterns. |
14 | | -- Ability to create custom parameter pattern. |
15 | | -- Namespace support. |
16 | | -- Route grouping support. |
17 | | -- Domain-based routing support. |
18 | | -- Ability to define custom 404 errors |
19 | | -- Ability to name routes |
20 | | -- Ability to call a class in (Symfony-like) callable functions or parameters of controller methods. |
21 | | -- Routing by request ports. |
22 | | -- Routing by client IP address (Via `$_SERVER['REMOTE_ADDR']`. Locally, this value can be something like `::1` or `127.0.0.1`.) |
23 | | -- A directory path can be defined as a virtual link. |
| 13 | +- GET, POST, PUT, DELETE, OPTIONS, PATCH, HEAD and ANY methods (plus virtual `LINK` routes). |
| 14 | +- Optional Laravel-style method override via `$_REQUEST['_method']`. |
| 15 | +- Controller handlers (`Home@about`, `Home::about`, `Home->about`, or `[Home::class, 'about']`). |
| 16 | +- Before/after middleware (filters), per route and per controller. |
| 17 | +- Static and dynamic route patterns, with a customizable pattern registry. |
| 18 | +- Route grouping by prefix, domain, port and client IP. |
| 19 | +- Named routes and URL generation. |
| 20 | +- Reflection-based dependency injection for handlers, with optional [PSR-11](https://www.php-fig.org/psr/psr-11/) container support. |
| 21 | +- Customizable 404 handling. |
| 22 | +- Serve files or whole directories as virtual links (with path-traversal protection). |
| 23 | +- Optional route caching. |
24 | 24 |
|
25 | 25 | ## Requirements |
26 | 26 |
|
27 | | -- PHP 7.2 or later |
28 | | -- Apache is; **AllowOverride All** should be set to and **mod_rewrite** should be on. |
29 | | -- Any library that implements the [Psr-7 HTTP Message Interface](https://www.php-fig.org/psr/psr-7/) and an emitter written for Psr-7. For example; [InitPHP HTTP](https://github.com/InitPHP/HTTP) Library |
| 27 | +- PHP 8.1 or later |
| 28 | +- Any [PSR-7](https://www.php-fig.org/psr/psr-7/) HTTP message implementation and a PSR-7 emitter. The examples below use [InitPHP HTTP](https://github.com/InitPHP/HTTP). |
| 29 | +- For pretty URLs, a front controller (`index.php`) with URL rewriting (see below). |
30 | 30 |
|
31 | 31 | ## Installation |
32 | 32 |
|
33 | | -``` |
| 33 | +```bash |
34 | 34 | composer require initphp/router |
35 | 35 | ``` |
36 | 36 |
|
37 | | -Is Apache `.htaccess` |
| 37 | +The examples in the documentation use the InitPHP HTTP library: |
38 | 38 |
|
| 39 | +```bash |
| 40 | +composer require initphp/http |
39 | 41 | ``` |
| 42 | + |
| 43 | +### Web server |
| 44 | + |
| 45 | +Apache (`.htaccess`): |
| 46 | + |
| 47 | +```apacheconf |
40 | 48 | RewriteEngine On |
41 | 49 | RewriteCond %{REQUEST_FILENAME} !-f |
42 | 50 | RewriteCond %{REQUEST_FILENAME} !-d |
43 | 51 | RewriteRule ^(.*)$ index.php [L] |
44 | 52 | ``` |
45 | 53 |
|
46 | | -Is NGINX; |
| 54 | +NGINX: |
47 | 55 |
|
48 | | -``` |
49 | | -server { |
50 | | - listen 80; |
51 | | - server_name myinitphpdomain.dev; |
52 | | - root /var/www/myInitPHPDomain/public; |
53 | | - index index.php; |
54 | | - location / { |
55 | | - try_files $uri $uri/ /index.php?$query_string; |
56 | | - } |
57 | | - location ~ \.php$ { |
58 | | - fastcgi_split_path_info ^(.+\.php)(/.+)$; |
59 | | - fastcgi_pass unix:/var/run/php7.4-fpm.sock; |
60 | | - fastcgi_index index.php; |
61 | | - include fastcgi.conf; |
62 | | - fastcgi_intercept_errors on; |
63 | | - } |
| 56 | +```nginx |
| 57 | +location / { |
| 58 | + try_files $uri $uri/ /index.php?$query_string; |
64 | 59 | } |
65 | 60 | ``` |
66 | 61 |
|
67 | | -## Configuration |
| 62 | +## Quick start |
68 | 63 |
|
69 | 64 | ```php |
70 | | -$config = [ |
71 | | - 'paths' => [ |
72 | | - 'controller' => null, //The full path to the directory where the Controller classes are kept. |
73 | | - 'middleware' => null, //The full path to the directory where the Middleware classes are kept. |
74 | | - ], |
75 | | - 'namespaces' => [ |
76 | | - 'controller' => null, //Namespace prefix of Controller classes, if applicable. |
77 | | - 'middleware' => null, //Namespace prefix of Middleware classes, if applicable. |
78 | | - ], |
79 | | - 'base_path' => '/', // If you are working in a subdirectory; identifies your working directory. |
80 | | - 'variable_method' => false, // It makes the request method mutable with Laravel-like $_REQUEST['_method']. |
81 | | - 'argument_new_instance' => false, // This configuration is used for Request and Response objects that you want as arguments. |
82 | | -]; |
83 | | -``` |
84 | | - |
85 | | -## Usage |
86 | | - |
87 | | -The following example uses the [InitPHP HTTP](https://github.com/InitPHP/HTTP) library. If you wish, you can use this library using the command below, or you can perform similar operations using another library that uses the Psr-7 HTTP Message interface. |
88 | | - |
89 | | -``` |
90 | | -composer require initphp/http |
91 | | -``` |
| 65 | +<?php |
| 66 | +require_once 'vendor/autoload.php'; |
92 | 67 |
|
93 | | -_**See the Wiki for detailed documentation.**_ |
94 | | - |
95 | | -```php |
96 | | -require_once "vendor/autoload.php"; |
97 | | -use \InitPHP\HTTP\Message\{Request, Response, Stream}; |
98 | | -use \InitPHP\HTTP\Emitter\Emitter; |
99 | | -use \InitPHP\Router\Router; |
| 68 | +use InitPHP\HTTP\Message\{Request, Response}; |
| 69 | +use InitPHP\HTTP\Emitter\Emitter; |
| 70 | +use InitPHP\Router\Router; |
100 | 71 |
|
101 | | -$request = Request::createFromGlobals(); |
| 72 | +$request = Request::createFromGlobals(); |
102 | 73 | $response = new Response(); |
103 | 74 |
|
104 | | -// Create the router object. |
105 | | -$router = new Router($request, $response, []); |
| 75 | +$router = new Router($request, $response); |
106 | 76 |
|
107 | | -// ... Create routes. |
108 | 77 | $router->get('/', function () { |
109 | 78 | return 'Hello World!'; |
110 | 79 | }); |
111 | 80 |
|
112 | 81 | $router->post('/login', function (Request $request, Response $response) { |
113 | | - return $response->json([ |
114 | | - 'status' => 0, |
115 | | - 'message' => 'Unauthorized', |
116 | | - ], 401); |
| 82 | + return $response->withStatus(401); |
117 | 83 | }); |
118 | 84 |
|
119 | | -// If you do not make a definition for 404 errors; An exception is thrown if there is no match with the request. |
120 | | -$router->error_404(function () { |
121 | | - echo 'Page Not Found'; |
| 85 | +// Optional: customise the 404 response (otherwise a PageNotFoundException is thrown). |
| 86 | +$router->setNotFoundHandler(function () { |
| 87 | + return 'Page Not Found'; |
122 | 88 | }); |
123 | 89 |
|
124 | | -// Resolve the current route and get the generated HTTP response object. |
| 90 | +// Resolve the current request and build the response. |
125 | 91 | $response = $router->dispatch(); |
126 | 92 |
|
127 | | -// Publish the HTTP response object. |
128 | | -$emitter = new Emitter; |
129 | | -$emitter->emit($response); |
| 93 | +// Emit it. |
| 94 | +(new Emitter())->emit($response); |
130 | 95 | ``` |
131 | 96 |
|
132 | | -## Getting Help |
| 97 | +## Documentation |
133 | 98 |
|
134 | | -If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker. |
| 99 | +Full, example-driven documentation lives in [`docs/`](./docs/README.md): |
135 | 100 |
|
136 | | -## Getting Involved |
| 101 | +- [Installation](./docs/installation.md) · [Getting started](./docs/getting-started.md) · [Configuration](./docs/configuration.md) |
| 102 | +- [Routing](./docs/routing.md) · [Route parameters & patterns](./docs/route-parameters.md) · [Route groups](./docs/groups.md) |
| 103 | +- [Controllers](./docs/controllers.md) · [Dependency injection](./docs/dependency-injection.md) · [Middleware](./docs/middleware.md) |
| 104 | +- [Named routes & URLs](./docs/named-routes.md) · [Static file links](./docs/static-file-links.md) |
| 105 | +- [Error handling](./docs/error-handling.md) · [Caching](./docs/caching.md) |
| 106 | +- [Upgrading from 1.x to 2.0](./docs/upgrading-1.x-to-2.0.md) |
137 | 107 |
|
138 | | -> All contributions to this project will be published under the MIT License. By submitting a pull request or filing a bug, issue, or feature request, you are agreeing to comply with this waiver of copyright interest. |
| 108 | +## Testing |
139 | 109 |
|
140 | | -There are two primary ways to help: |
141 | | - |
142 | | -- Using the issue tracker, and |
143 | | -- Changing the code-base. |
144 | | - |
145 | | -### Using the issue tracker |
146 | | - |
147 | | -Use the issue tracker to suggest feature requests, report bugs, and ask questions. This is also a great way to connect with the developers of the project as well as others who are interested in this solution. |
148 | | - |
149 | | -Use the issue tracker to find ways to contribute. Find a bug or a feature, mention in the issue that you will take on that effort, then follow the Changing the code-base guidance below. |
| 110 | +```bash |
| 111 | +composer test # PHPUnit |
| 112 | +composer phpstan # static analysis (level max) |
| 113 | +composer cs:check # coding standards (PSR-12) |
| 114 | +composer coverage:check # line coverage with an enforced floor |
| 115 | +``` |
150 | 116 |
|
151 | | -### Changing the code-base |
| 117 | +## Contributing |
152 | 118 |
|
153 | | -Generally speaking, you should fork this repository, make changes in your own fork, and then submit a pull request. All new code should have associated unit tests that validate implemented features and the presence or lack of defects. Additionally, the code should follow any stylistic and architectural guidelines prescribed by the project. In the absence of such guidelines, mimic the styles and patterns in the existing code-base. |
| 119 | +Contributions are welcome. Please read the org-wide |
| 120 | +[Contributing guide](https://github.com/InitPHP/.github/blob/main/CONTRIBUTING.md) |
| 121 | +and the [Code of Conduct](https://github.com/InitPHP/.github/blob/main/CODE_OF_CONDUCT.md) |
| 122 | +before opening a pull request. |
154 | 123 |
|
155 | 124 | ## Credits |
156 | 125 |
|
157 | 126 | - [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<info@muhammetsafak.com.tr>> |
158 | 127 |
|
159 | 128 | ## License |
160 | 129 |
|
161 | | -Copyright © 2022 [MIT Licence](./LICENSE) |
| 130 | +Released under the [MIT License](./LICENSE). |
0 commit comments