klein / klein.php

A fast & flexible router
MIT License
2.66k stars 290 forks source link

Function Variables #258

Open karimkawambwa opened 9 years ago

karimkawambwa commented 9 years ago

Just for helping coding style, can the function variables be returned in and single object. Eg.

$klein->respond('GET', '/login',
function (Request $request, Response $response, ServiceProvider $service, App $app) ...

is really long, if i only just want to get $app. So can this be made better by perhaps having them all in one object parameter e.g.

$klein->respond('GET', '/login',
function ($object) ... {
$object->app ...
$object->request ... 
$object->response ...
$object->service ...

or

$klein->respond('GET', '/login',
function ( /*Parameters from request passed here*/ ) ... use($object) {
$object->app ...
$object->request ... 
$object->response ...
$object->service ...

or which ever way is better.

Rican7 commented 9 years ago

Yea, this is something I have planned for a later version. Unfortunately, it would break backwards compatibility pretty heavily, as all route callbacks would have to have their signatures changed, so its going to have to wait until the next major version.

nbish11 commented 9 years ago

@Rican7,

I was just wandering if a simple if/else could be used.

if (true === $this->passKleinToCallback) {
    $returned = call_user_func(
        $route->getCallback(),
        $this,
        $matched,
        $methods_matched
    );
} else {
    // Handle the callback
    $returned = call_user_func(
        $route->getCallback(), // Instead of relying on the slower "invoke" magic
        $this->request,
        $this->response,
        $this->service,
        $this->app,
        $this, // Pass the Klein instance
        $matched,
        $methods_matched
    );
}

It's not pretty, but it works. If you do want do this, let me know and I'll create a pull request.

Rican7 commented 9 years ago

I'd rather not introduce more configurable branching for now. Klein already has a lot of branching based on configurations and injectables. I might introduce an interesting way to do this in a refactor that I'm current working on. We'll see.

nbish11 commented 9 years ago

@Rican7 Fair enough.