I am using slim framework (routing etc) and illuminate database to output data on my webpage but it does not seem to be working as the php code keeps getting commented out when i load the webpage in browser (chrome).
my routing (I am using slim-twig as well):
$app->get('/', function ($request, $response) {
return $this->get('view')->render($response, 'index.php');
});
php code I am trying to render in index.php
<?php
require "/vendor/autoload.php";
$tournaments = Tournaments::all();
phpinfo();
foreach ($tournaments as $tournament) {
echo $tournament->name;
}
?>
^ this code comes out as <!-- code -->
in browser code view.
I am new to php frameworks so explanation would be appreciated .
The "View" layer is not responsible to fetch the data. Instead you should pass all the data to view with the 3. parameter.
$app->get('/', function ($request, $response) {
$viewData = [
'tournaments' => Tournaments::all(),
];
return $this->get('view')->render($response, 'index.twig', $viewData);
});
index.twig
{% for tournament in tournaments %}
{{ tournament.name}}<br>
{% endfor %}