https://github.com/jordanbrauer/php-router
An example PHP router/dispatcher system for learning purposes.
https://github.com/jordanbrauer/php-router
composer oop php router
Last synced: about 1 month ago
JSON representation
An example PHP router/dispatcher system for learning purposes.
- Host: GitHub
- URL: https://github.com/jordanbrauer/php-router
- Owner: jordanbrauer
- License: mit
- Created: 2017-04-19T17:41:12.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-04-23T04:28:53.000Z (over 8 years ago)
- Last Synced: 2025-01-10T20:53:08.720Z (10 months ago)
- Topics: composer, oop, php, router
- Language: PHP
- Size: 9.77 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# php-router
An example (very simple) PHP router/dispatcher system for learning purposes.
## Setup
```shell
$ git clone https://github.com/jordanbrauer/php-router.git
$ cd ./php-router
$ composer install
```
## Usage
### Boilerplate
Start by requiring the `autoload.php` file, using the class, and instantiating a new router like so,
```php
require_once 'vendor/autoload.php';
use \Jorb\Router\Router as Router;
$router = new Router();
```
### API
Now that you have a new router you can start using, it is time to read about the routers' API and put it to the test!
#### `add()`
> `Router::add(string $route, mixed $method);`
Add a route to the router.
__Example:__ obligartory hello world
```php
$router->add('/', function () {
echo 'Hello World!';
});
```
__Example:__ Route w/ argument
```php
$router->add('/user/.+', function ($user) {
echo "Welcome {$user}!";
});
```
__Example:__ Route w/ many arguments
_Note: this feature is currently bugged and does not work as intended. The dispatch method will match multiple routes if a longer route such as the one below contains an already existing shorter route in its' path._
```php
$router->add('/user/.+/repository/.+', function($user, $repository) {
echo "Repository: {$user}/{$repository}";
})
```
#### `dispatch()`
> `Router::dispatch();`
Route the user to all appropriate destinations when requested.
__Example:__
```php
$router->dispatch();
```