An open API service indexing awesome lists of open source software.

https://github.com/funkybunch/simple-http-router

A Simple Server-Side HTTP Request Router written in PHP. This was built to be intended for APIs but will work for Web UI traffic as well.
https://github.com/funkybunch/simple-http-router

api api-router http http-requests php-router php-routing router

Last synced: 10 months ago
JSON representation

A Simple Server-Side HTTP Request Router written in PHP. This was built to be intended for APIs but will work for Web UI traffic as well.

Awesome Lists containing this project

README

          

*2020 Update:* PHP will forever have a special place in my heart, but its 2020 and I have moved onto other projects. As such this repository will no longer be maintained or updated. This repo and code will remain available for anyone to use, fork, modify to your heart's content, but should be considered "No longer supported".

# Simple HTTP Router
[![Build Status](https://travis-ci.org/funkybunch/Simple-HTTP-Router.svg?branch=master)](https://travis-ci.org/funkybunch/Simple-HTTP-Router)

A simple router for HTTP requests written in PHP. Simple HTTP Router allows you to have a single point of entry for your web application or API and explicitly call different functions based on the HTTP `REQUEST_METHOD` and `REQUEST_URI`.

## Installation
#### Simplest (Composer)
The quickest way to get up and running is to use [Composer](https://github.com/composer/composer).

1. Once you have Composer installed, initialize it in your root project directory using:
```sh
$ composer init
```
2. Now you're all ready to start adding some dependencies. To install Simple HTTP Router:
```sh
$ composer require funkybunch/simple-http-router
```

That's it! Just make sure to `autoload` at the top of your code.
```php
get($path, function(){});
```

##### Here's what your `index.php` file might look like at this point:
```php
get('/', function() {
// The following code will be executed when this `route` is called.
echo "

Hello world

";
});

$router->get('/about', function() {
// The following code will be executed when this `route` is called.
echo "

About Us

";
});
```

#### `POST` Request
To setup a route for a `POST` request, use the `post()` method in the `Router()` class. Similarly to the `get()` method, this method requires the `$path` that you are setting the route for, as well as an anonymous `function()`. This `function()` is called if the `$path` matches the HTTP request.
```php
$router->post($path, function(){});
```
##### Here's what your `index.php` file might look like at this point:
```php
get('/', function() {
// The following code will be executed when this `route` is called.
echo "

Hello world

";
});

$router->get('/about', function() {
// The following code will be executed when this `route` is called.
echo "

About Us

";
});

$router->post('/api/contact', function() {
// The following code will be executed when this `route` is called.
// Handle `POST` data
});
```

#### `getRequest()` Method
If you are building an API, you will likely need to get the `body` of the HTTP request. This can be done using the `getRequest()` method. This returns the request object for both `GET` and `POST` requests.
```php
$router->getRequest()
```

It is possible to use this method inside the `callback` function for either the `get()` or `post()` methods using a [closure](http://php.net/closure). For example:
```php
$router->post($path, function() use(&$router) {
$request = $router->getRequest();
// DO SOMETHING WITH $request
}
```
The `&` is important because it tells PHP to use the global variable that you have already defined.

##### Here's what your `index.php` file might look like at this point:
```php
get('/', function() {
// The following code will be executed when this `route` is called.
echo "

Hello world

";
});

$router->get('/about', function() {
// The following code will be executed when this `route` is called.
echo "

About Us

";
});

$router->post('/api/contact', function() use(&$router) {
// The following code will be executed when this `route` is called.
$request = $router->getRequest();

// DO SOMETHING WITH $request to read POST data
});
```

**Note:** Even if Apache, or your favorite web server, is serving `/var/www/mywebapp/web/` then it can still reference files in `/var/www/mywebapp/`. This way you can keep the rest of your application files out of a live web directory. The important part is that the file using the router is in the live directory.

## Contributing & Bugs
Contributions are always welcome! Draft a [pull request](https://github.com/funkybunch/Simple-HTTP-Router/pulls) with any changes or improvements you make and submit it for review.

For bug reports, please create an [issue](https://github.com/funkybunch/Simple-HTTP-Router/issues) and I will look into it further.

## Roadmap
- Advanced Error States & Handling

Please create an [issue](https://github.com/funkybunch/Simple-HTTP-Router/issues) for any other suggestions.

## License
Copyright (c) 2019 Mark Adkins and other [contributors](https://github.com/funkybunch/Simple-HTTP-Router/graphs/contributors). This project is Open Source and licensed under the [MIT License](https://github.com/funkybunch/Simple-HTTP-Router/blob/master/LICENSE).