The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Currying a non-anonymous function (uses($app))

Robert's Avatar

Robert

23 Jul, 2015 03:49 AM

I'm have a function for a route that is going to be reused so I broke it out of the $app->get route and went from this:

$app->get('/movie', function($id) use($app){
   //things
});
to
$app->get('/movie', 'getMovieByID');
function getMovieByID($id){
   //things
}
but how can I get access to $app within my 'getMovieByID' function? I also don't really understand currying very well as it is a new concept to me, but is there some sort of accepted way to accomplish what I'm looking for?
  1. Support Staff 1 Posted by Andrew Smith on 29 Jul, 2015 10:37 AM

    Andrew Smith's Avatar

    If this is Slim 2, you can pass it in through a not documented method called setParams.

    $app->get('/movie', 'getMovieByID')->setParams([$app]);
    function getMovieByID($app, $id){
       //things
    }
    

    I haven't tested this with a single function before, only with methods in classes but it should work.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac