2018-03-12 10:49:55 -05:00
# Simple PHP Router
2019-07-24 10:56:58 -05:00
Hey! This is a simple and small PHP router that can handle the whole URL routing for your project.
It utilizes RegExp and PHP's anonymous functions to create a lightweight and fast routing system.
The router supports dynamic path parameters, special 404 and 405 routes as well as verification of request methods like GET, POST, PUT, DELETE, etc.
The codebase is very small and very easy to understand. So you can use it as a boilerplate for a more complex router.
2018-03-12 10:49:55 -05:00
2020-01-08 09:25:45 -06:00
Take a look at the index.php file. As you can see the `Route::add()` method is used to add new routes to your project.
2020-01-10 15:50:16 -06:00
The first argument takes the path segment. You can also use RegExp in there to parse out variables.
2018-10-25 05:53:28 -05:00
All matching variables will be pushed to the handler method defined in the second argument.
The third argument will match the request method. The default method is 'get'.
2018-03-12 10:49:55 -05:00
2018-03-13 10:01:52 -05:00
## Simple example:
2020-01-08 09:25:45 -06:00
```php
2020-01-14 04:43:16 -06:00
use Steampixel\Route;
2020-01-14 05:03:03 -06:00
include 'src\Steampixel\Route.php';
2018-03-13 10:01:52 -05:00
2020-01-08 09:25:45 -06:00
Route::add('/user/([0-9]*)/edit', function($id) {
2019-07-24 10:56:58 -05:00
echo 'Edit user with id '.$id.'< br > ';
}, 'get');
2018-03-13 10:01:52 -05:00
Route::run('/');
2018-03-12 10:49:55 -05:00
```
2018-03-13 10:01:52 -05:00
You will find a more complex example with a build in navigation in the index.php file.
## Use a different basepath
2019-07-24 10:56:58 -05:00
If your script lives in a subfolder (e.g. /api/v1) set this basepath in your run method:
2018-03-13 10:05:11 -05:00
2020-01-08 09:25:45 -06:00
```php
Route::run('/api/v1');
```
2018-03-13 10:05:11 -05:00
2018-10-25 05:55:04 -05:00
Do not forget to edit the basepath in .htaccess too if you are on Apache2. In order to run the test files correctly inside a basepath you should also adjust the navigation links inside the index.php.
2018-03-12 11:12:05 -05:00
2020-01-11 15:44:56 -06:00
## Enable case sensitive routes, trailing slashes and multi match mode
2020-01-11 15:57:18 -06:00
The second, third and fourth parameters of `Route::run('/', false, false, false);` are set to false by default.
2020-01-11 15:56:41 -06:00
Using this parameters you can switch on and off several options:
* Second parameter: You can enable case sensitive mode by setting the second parameter to true.
* Third parameter: By default the router will ignore trailing slashes. Set this parameter to true to avoid this.
* Fourth parameter: By default the router will only execute the first matching route. Set this parameter to true to enable multi match mode.
2019-01-31 03:54:07 -06:00
2018-03-12 11:12:05 -05:00
## Something does not work?
2020-01-08 09:25:45 -06:00
* Don't forget to set the correct basepath as the first argument in your `run()` method and in your .htaccess file.
* Enable mod_rewrite in your Apache2 settings, in case you're using Apache2: `a2enmod apache2`
2018-03-20 10:07:26 -05:00
2019-01-31 04:04:03 -06:00
## Test setup with Docker
I have created a little Docker test setup.
2019-01-31 02:24:54 -06:00
2020-01-10 15:50:16 -06:00
1. Build the image: `docker build -t simplephprouter docker/image-php-7.2`
2019-01-31 02:24:54 -06:00
2. Spin up a container
2020-01-08 09:25:45 -06:00
* On Linux / Mac or Windows Powershell use: `docker run -d -p 80:80 -v $(pwd):/var/www/html --name simplephprouter simplephprouter`
* On Windows CMD use `docker run -d -p 80:80 -v %cd%:/var/www/html --name simplephprouter simplephprouter`
2019-01-31 02:24:54 -06:00
3. Open your browser and navigate to http://localhost
2019-03-13 10:06:51 -05:00
## Themes, layouts, pages and components
If you are interested in some basic concepts on how to build a simple PHP page using this router including themes, layouts, pages and components checkout this repo: https://github.com/steampixel/simplePHPPages
2020-01-08 09:25:45 -06:00
This project will give you some ideas and basics on how to get started without any dependencies.
2019-03-13 10:06:51 -05:00
2018-07-25 06:44:37 -05:00
## Todo
* Create demo configuration for nginx
2019-07-24 10:56:58 -05:00
* Create Composer configuration and upload to packagist.org
2018-07-25 06:44:37 -05:00
2019-07-24 10:56:58 -05:00
## License
This project is licensed under the MIT License. See LICENSE for further information.