我使用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框架,所以解释将被感激。
视图 "层不负责获取数据。相反,你应该用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 %}