c9s/Pux — pull request #36 by mway (2014-02-14 21:50:09+00:00)
Title: Expanded controller HTTP methods

Hello! I started using Pux a while back but ran into some issues with URI collisions and using `$controller->expand()` with HTTP methods, so thought I'd make some tweaks. I haven't written/contributed to a PHP extension before, so I apologize in advance if any of the Zend API idioms or general extension best practices aren't observed - please let me know if there's anything you'd like rewritten.

### Purpose
The purpose of this change is to allow registering HTTP method-specific controller actions via `\Pux\Controller::expand()`, and also allow those actions to share the same URIs (previously they would overwrite each other). This is especially useful for using Pux as a router for a micro REST framework, e.g., not needing to use separate URIs (e.g., `/users` + `/users/create`) where there is generally only one (`/users`). With these changes, it is possible to do something like

mount(null, $c->expand());

and then:

curl localhost/
#=> Index!

curl -X localhost/
#=> Index post!

rather than either explicitly registering each route with `\Pux\Mux`, or detecting `$_SERVER['REQUEST_METHOD']` in the action and potentially needing to either a) mix code with significantly different intentions or b) make additional method calls to separate it out.

### PHP + extension changes
1. `Controller::getActionPaths()` members now include a normalized HTTP method as a string (GET|PUT|POST|DELETE)
- Note this HTTP method is not included in the URI, so endpoints can be shared (e.g., GET /comments for listing vs. POST /comments for creating)
2. Changed `Mux::mount()` to:
- Enforce at least a root URI (/) if null or an empty string is passed, instead of warning
- Merge both the passed `$options` and route options arrays when adding each route
3. Changed `Controller::expand()` to:
- Now read `$options` array from `Controller::getActionPaths()` result
- Will translate the HTTP method string (if provided) to the int constant expected by `Mux::add()`
- Now passes `$options` to `Mux::add()`

### Extension-only changes
1. Added `Mux::getRequestMethodConstant()` (previously only existed in PHP)