0

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

menjaraz
  • 7,551
  • 4
  • 41
  • 81
kristian nissen
  • 2,809
  • 5
  • 44
  • 68

1 Answers1

4

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');
igorw
  • 27,759
  • 5
  • 78
  • 90
  • Ah... I wasn't sure if I was missing some call to Pimple. I should have tested my code in my answer first. This answer should be marked as excepted – Adam Elsodaney Mar 16 '13 at 17:00
  • What is the benefit of using $app->protect() instead of creating a static helper method and use it anywhere in the code? – Mohammad Sharaf Ali Feb 22 '17 at 13:32