在使用slim框架时,PHP代码无法执行(已注释)。

问题描述 投票:0回答:1

我使用slim框架(路由等)和illuminate数据库在我的网页上输出数据,但是当我在浏览器(chrome)中加载网页时,php代码一直被注释掉,似乎无法工作。

我的路由(我也在使用slim-twig)。

$app->get('/', function ($request, $response) {
    return $this->get('view')->render($response, 'index.php');
});

我试图在index.php中呈现的php代码是这样的

<?php

    require "/vendor/autoload.php";

    $tournaments = Tournaments::all();

    phpinfo();

    foreach ($tournaments as $tournament) {
        echo $tournament->name;
    }

?>

^这段代码的输出是 <!-- code --> 在浏览器代码视图中。

我是一个新的PHP框架,所以解释将被感激。

php slim slim-3
1个回答
0
投票

视图 "层不负责获取数据。相反,你应该用3.参数把所有数据传给view。

$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 %}
© www.soinside.com 2019 - 2024. All rights reserved.