ZeffMu icon indicating copy to clipboard operation
ZeffMu copied to clipboard

View helpers and Controller plugin helpers in closures

Open Ocramius opened this issue 12 years ago • 6 comments

The ZeffMu\App class should expose methods to allow accessing view and controller helpers in closures. Ideally something like following I suppose:

$app = \ZeffMu\App::init();
$app
    ->route('/', function() use ($app) {
        $form = new Form(/* yaddayadda */);
        return $app->viewHelper()->form($someForm);
    })
    ->route('/login', function() use ($app) {
        return $app->controllerHelper()->redirect('/');
    })

Ocramius avatar Jan 12 '13 23:01 Ocramius

Is it ok to limit this functionality to PHP 5.4? if so then we just need to run some more tests on various accesses.

Currently, due to the upgrade/change to the ClosureController and the bindTo, $this inside the closure under PHP 5.4 is the instance of the ClosureController. This should give access to the controller methods (need another test for that to be certain)

Thoughts?

BinaryKitten avatar Feb 05 '13 11:02 BinaryKitten

Well, this can work with both:

5.3:

->route('/', function (/* ... */, $controller) {
    $controller->doStuff();
});

5.4:

->route('/', function () {
    $this->doStuff();
});

Ocramius avatar Feb 05 '13 11:02 Ocramius

hmm,

maybe something like an update to app to store the event rather than pushing more into the args?

so App::getDispatchedEvent()

which would return the event, from which the controller could be got

5.3

->route('/', function () use ($app) {
    $event = $app->getDispatchedEvent();
    $controller = $event->getTarget();
    $controller->doStuff();
});

5.4 would remain the same

->route('/', function () use ($app) {
    $event = $app->getDispatchedEvent();
    $controller = $this;
    $controller->doStuff();
});

BinaryKitten avatar Feb 05 '13 12:02 BinaryKitten

Hmm... A bit too verbose, no? :) But anyway, I think there's no problem if the feature is left out for 5.3 for now. Ralph's method would allow us to add an arbitrary amount of parameters to the closures though

Ocramius avatar Feb 05 '13 12:02 Ocramius

is being "too verbose" a problem?

On Tue, Feb 5, 2013 at 12:47 PM, Marco Pivetta [email protected]:

Hmm... A bit too verbose, no? :) But anyway, I think there's no problem if the feature is left out for 5.3 for now. Ralph's method would allow us to add an arbitrary amount of parameters to the closures though

— Reply to this email directly or view it on GitHubhttps://github.com/BinaryKitten/ZeffMu/issues/7#issuecomment-13127660.

BinaryKitten avatar Feb 05 '13 12:02 BinaryKitten

@BinaryKitten well, it's a micro-framework ;)

Ocramius avatar Feb 05 '13 12:02 Ocramius