silexpimple

Silex Pimple service implementation


Inside my Silex app I need a function which basically does a file_get_contents() my idea was to use something like

$app['funky_service'] = function () {
$content = file_get_contents();
return $content;
}

this is working fine, but how can I pass parameters to this function? I can call it like this

$fs = $app['funky_service'];

but passing arguments to it is still puzzling my


Solution

  • As per the services chapter in the silex documentation, you need to protect your function if you want to store it as a parameter:

    $app['function_parameter'] = $app->protect(function ($resource) {
        $content = file_get_contents($resource);
        return $content;
    });
    
    $example = $app['function_parameter']('http://example.com');