Closed nikrowell closed 5 years ago
Or for a slightly more sophisticated approach that detects route params and passes those into the method call, replace the $callback
closure above with this:
$reflection = new ReflectionMethod($class, $method);
$params = (array) $reflection->getParameters()[0];
$params = $req->paramsNamed()->all(array_values($params));
$controller = new $class($req, $res, $ser, $app);
$reflection->invokeArgs($controller, $params);
@rekcuFniarB thanks for sharing!
Thanks for all work on this @Rican7 ! I'm interested in adding support for Laravel-style 'Controller@method' route callbacks. Something like this:
... which could be implemented by adding the following to
setCallback
in Route.php:I know there is support for static controller methods, but this technique also opens up some interesting possibilities for using a base Controller class, adding middleware etc. I'd be happy to submit this as a Pull Request with tests etc if this is something you'd be open to adding to Klien.php. It doesn't look like this project is getting much activity in terms of recent commits, but I think it's delightful to use on small projects and would love to see it continue to be maintained.