Closed GreeKatrina closed 9 years ago
Hi @GreeKatrina.
From looking at your code it looks like you're just missing a small bit here, which you've seemed to catch on to considering your attempt to fix it.
PHP's "closure" implementation is pretty unique, in that the variables that are allowed to be accessed inside the closure have to be manually denoted. So it really looks like you just need to tell your closures to "use" the $controller
variable from the parent scope... like this:
$controller = new $controller_name;
$router->get('/?', function($req, $res, $service, $app) use ($controller) { return $controller->index($req, $res, $service, $app); });
// ....
... Having said that, I wouldn't suggest dynamically building and calling classes in this manner. Doing things like this is quite "magical" and ends up hiding a lot of implementation details that might make it harder for a team member (or even yourself in a few months) to understand. This kind of behavior can also limit testability pretty heavily.
If you'd like to use a more dynamic approach without hiding the inner complexity while still allowing for a more testable setup, I'd suggest either manually defining all of the routes or using a factory pattern of some sort (kind of like what was described in #279).
I hope Klein suits your needs and you enjoy using it. :)
Thank you so much @Rican7. That was exactly what I needed. #279 is helpful also. I think I'll go with the Controller Factory as you suggested. As someone who moved from Ruby to PHP, Klein was awesome to find.
Oh awesome! I'm glad that helped. :)
Thanks for the kind words! Good luck with your project. :+1:
If this is already do-able, but I couldn't figure it out, I apologize in advance.
I would like to be able to do something like this:
Currently when I do this, I will get errors like "undefined variable $controller". If I try to pass $controller into the callback like:
It throws an error too. Not sure of the best way to go about this, or if it's even currently possible to do. Thanks for the help and this library is great. I was looking forever for a router that was light and didn't take forever to set up.