简体   繁体   中英

PHP code not being executed (commented out) while using slim framework

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM