anandkunal / ToroPHP

Toro is a PHP router for developing RESTful web applications and APIs.
http://toroweb.org
MIT License
1.17k stars 173 forks source link

Ability to pass custom method names in handler #77

Closed quickliketurtle closed 10 years ago

quickliketurtle commented 10 years ago

Add ability to override the default behavior of calling method matching HTTP request type with custom method by appending '@methodName' to the handler. Updated readme with example usage.

petemcfarlane commented 10 years ago

This would be a useful feature, it could mean having a single handler for any collection, but with multiple methods, e.g. call list and show on the same collection and both with a http get request

martinbean commented 10 years ago

Using the same handler for viewing a collection and viewing an item can already be achieved simply:

<?php
class CollectionHandler {
    public function get($id = null) {
        if (is_null($id)) {
            // No ID, view collection
        } else {
            // View item specified by ID
        }
    }
}

Toro::serve(array(
    'collection' => 'CollectionHandler',
    'collection/:numeric' => 'CollectionHandler'
));
quickliketurtle commented 10 years ago

Hi Martin, this lets you use more restful methods (index, show, create, edit, update, destroy...) and custom methods (processHash...).

That being said, i didn't ask it this was useful before submitting the PR so i understand it not being merged.

martinbean commented 10 years ago

HI @quickliketurtle. No worries. It’s more just to keep the barrier of entry for ToroPHP low.

I’m very fond of resource controllers in Laravel (which have methods named as you’ve described: index, show, create, edit, update, and destroy, so don’t know if that’s your inspiration), but those names require a more in-depth knowledge of REST conventions; whereas most developers (should) know the different between GET and POST, and when to use each appropriately.