Question

while following this bit of code

    $f3=require('lib/base.php');

    $f3->route('GET /brew/@count',
        function($f3) {
            echo $f3->get('PARAMS.count').' bottles of beer on the wall.';
        }
    );
    $f3->run();

from Fat-Free Framework examples at https://github.com/bcosca/fatfree#routing-engine I get this error in my browser:

    Internal Server Error

    Missing argument 1 for {closure}()

    * /var/www/f3/index.php:36 Base->{closure}
    * /var/www/f3/lib/base.php:972         
    * Base->call(Closure::__set_state(),NULL,'beforeroute,afterroute')
    * /var/www/f3/index.php:40 Base->run()

I know the problem is that $f3 is not passed to function as parameter but i don't understand why. Can anybody help me?

Was it helpful?

Solution

I just the same situation! I was using version 3.0.1 from SourceForge. I tried the following, which worked, but created other issues:

$f3=require('lib/base.php');

$f3->route('GET /brew/@count',
    function() use ($f3) {
        echo $f3->get('PARAMS.count').' bottles of beer on the wall.';
    }
);
$f3->run();

The use clause (which isn't very well documented in php) allows you to use variables from the parent scope from an anonymous function. Read more here: Closure vs Anonymous function (difference?)

Try upgrading the F3 core to the latest release. One of the developers recommended to upgrade to the 3.0.2 release from here: https://github.com/bcosca/fatfree. It was a very quick fix!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top