Alan T
Alan T

Reputation: 11

500 error when an unset framework variable is used in template

I got a mysterious error on the page routed to MainController::somefunction

Internal Server Error
Unable to open

[vendor/bcosca/fatfree/lib/base.php:2315] Base->error()
[vendor/bcosca/fatfree/lib/base.php:3130] user_error()
[tmp/1esys3p2sx9xp.3h31n1yj254w8.php:2] Preview->render()
[vendor/bcosca/fatfree/lib/base.php:2875] require('/opt/lampp/htdocs/project/tmp/1esys3p2sx9xp.3h31n1yj254w8.php')
[vendor/bcosca/fatfree/lib/base.php:3121] View->sandbox()
[app/controllers/MainController.php:41] Preview->render()
[vendor/bcosca/fatfree/lib/base.php:1928] MainController->somefunction()
[vendor/bcosca/fatfree/lib/base.php:1728] Base->call()
[index.php:12] Base->run()

In my MainController::somefunction() a variable is set to a template

$this->f3->set('content', 'products.htm');
echo \Template::instance()->render('layout.htm');

And in layout.htm has the following line

    <include href="{{ @contents }}" />

The error message did not provide much useful detail. It took me a while to find out that the variable is defined as 'content' in the function but the template referenced 'contents' -- there is a missed 's'.

It would be more helpful if the error could say sth like "undefined variable". Maybe this information will find someone scratching their head.

Upvotes: 1

Views: 602

Answers (2)

Raphael MOUQUIN
Raphael MOUQUIN

Reputation: 1

@Alan T, @alan-t , right ; just starting with F3 this week i scratched my head 2 days ago with this same issue ; my 2 cents : when something like thing happens, whatever framework or library you are using, proceed with a file search project wide ; for instance in your case you can do a search for "Unable to open" to see where exactly it bugs , and i've been lucky to find the culprid line thanks to the trace with the exact line number displayed.(not the case in your issue/stact trace)

BIG Thanks to @Rayne for all this precious info regarding 'calling error_reporting(E_ALL) after loading F3'
and especially regarding promoting warnings to exceptions; i'm going to update my code base right after this reply

Thanks all to help beginners with what seems to me so far a very efficient packed framework! And the official doc is full of clever code samples when we take time to dig into it! Thanks to all f3 folks

Best regards Raphael

Upvotes: 0

Rayne
Rayne

Reputation: 2660

The primary issue is that your PHP configuration is ignoring "minor issues" like undefined variables. Let PHP report all kinds of issues by calling error_reporting(E_ALL) (after loading F3). Technically, E_NOTICE is enough to report undefined variables. As soon as this flag is set, PHP can show/log (depends on your configuration) a warning and Fat-Free Framework's ONERROR handler can react to undefined variables

Undefined variable: contents
[tmp/21hg1fh3jezo8.1gkglltf97qxb.php:1] Base->{closure}()
[test.php:16] Preview->render()

I recommend to promote warnings to exceptions, too. A good example is shown in a comment of the documentation page of set_error_handler(). This change forces you to program defensively and reveals issue that otherwise would go either unnoticed or result in unexpected side-effects (like your issue).

Feel free to create a pull request against bcosca/fatfree-core that generates a helpful error message when a null file is passed to the template renderer.

Upvotes: 1

Related Questions