SimpleRouter/README.md

66 lines
2.5 KiB
Markdown
Raw Normal View History

2021-07-14 16:16:33 -05:00
# SimpleRouter, PHP Edition
2018-03-12 10:49:55 -05:00
2021-07-14 16:16:33 -05:00
Aloha! SimpleRouter is a super-small, lightweight, and easy-to-use router for your PHP project. It can handle any type of request, and features RegEx pattern matching for URI parameters. You can also easily define routes for 404 and 405 errors.
2018-03-12 10:49:55 -05:00
2021-07-14 16:16:33 -05:00
As this implementation is very simple, it works great as boilerplate for a more complicated router if your project demands it. I created this to serve as a basic router for a small RPG game in PHP. Let me know what you use it for in the Discussions tab!
2018-03-12 10:49:55 -05:00
2021-07-14 16:16:33 -05:00
## Usage
```php
// Include the class... (this can also be done via autoloading)
2021-07-14 16:16:33 -05:00
include 'src\Splashsky\Router.php';
2018-03-13 10:01:52 -05:00
// Use the namespace...
2021-07-14 16:16:33 -05:00
use Splashsky\Router;
2020-01-14 06:00:23 -06:00
// Add the first GET route...
Router::get('/user/{id}/edit', function ($id) {
return 'Edit user with id '.$id.'<br>';
});
2018-03-13 10:01:52 -05:00
// Run the router!
Router::run();
2018-03-12 10:49:55 -05:00
```
2021-07-14 16:16:33 -05:00
## Installation
2021-07-14 16:16:33 -05:00
The easiest way to use SimpleRouter is to install it in your project via Composer.
2021-07-14 16:16:33 -05:00
```bash
composer require splashsky/simplerouter-php
```
2021-07-14 16:16:33 -05:00
Otherwise, download the latest Release and use `include` or `require` in your code.
## Caveats
Using SimpleRouter is... simple! There's a couple of principles to note, however.
### Root Route
You can't have an empty route (`Router::get('', ...);`), as the router **always assumes you at least have a `/` in your URI**. The root route should always be `/`, such as in `Router::get('/', function () {});`.
### Parameters are in order they appear
In the example of `/api/hello/{name}`, your first instinct when getting this parmeter in your action is that the variable will be named `$name`. This isn't the case - route parameters are in the order they are found in the route, and names are irrelevant.
2021-07-14 16:16:33 -05:00
## Routing for subfolders
If you're wanting to route for seperate uses (such as an api), you can create another entrypoint (in `/api/v1` for example) and pass a custom base path to the router.
2021-03-22 03:11:59 -05:00
```php
Router::run('/api/v1');
2021-03-22 03:11:59 -05:00
```
2021-07-14 16:16:33 -05:00
Ensure that your web server points traffic from `/api/v1` to this entrypoint appropriately.
2021-07-14 16:16:33 -05:00
## Contributing
2018-03-20 10:07:26 -05:00
2021-07-14 16:16:33 -05:00
I'm happy to look over and review any Issues or Pull Requests for this project. If you've run into a problem or have an enhancement or fix, submit it! It's my goal to answer and review everything within 48 hours.
2021-07-14 16:16:33 -05:00
## Credit
2020-09-07 03:07:46 -05:00
2021-07-14 16:16:33 -05:00
Most of the code so far has been initially written by [@SteamPixel](https://github.com/steampixel), so make sure to give him a follow or a star on the original repo as well. Thanks!
2018-07-25 06:44:37 -05:00
2019-07-24 10:56:58 -05:00
## License
2021-07-14 16:16:33 -05:00
2019-07-24 10:56:58 -05:00
This project is licensed under the MIT License. See LICENSE for further information.