Respect / Rest

Thin controller for RESTful applications
http://respect.github.io/Rest
Other
605 stars 102 forks source link

Bind specific controller methods to route names #110

Closed endel closed 10 years ago

endel commented 10 years ago

Hello,

It would be nice to allow binding routes to specific controller methods, just like Laravel does. It isn't possible today, right?

Example: (docs)

Route::get('user/{id}', 'UserController@showProfile');

I'm aware of the existence of illuminate/routing, I've already tried it, but Respect/Rest is far more performative due it's lightweight nature.

alganet commented 10 years ago

If I got this right, yeah! You can do it! Method calls will be static though:

<?php

// Will call a static showProfile on the UsersController class.
$router->get('/users/*', ['UsersController', 'showProfile']);

Respect\Rest looks for valid callable arguments. On PHP there is a lot of ways of creating callable arguments. It's not Respect specific:

// Calls the static DateTime::createFromFormat
call_user_func(['DateTime', 'createFromFormat'], 'd-m-Y', '10-10-2012');

You'll need to create the instance for yourself if you don't want static calls:

<?php

$usersController = new UsersController;
// Will call a standard showProfile on the UsersController class.
$router->get('/users/*', [$usersController, 'showProfile']);

Using this way to bind your controller classes also don't need the Routable interface, because the calls are treated as callbacks.

endel commented 10 years ago

Awesome @alganet, that's it. Thanks!