【问题标题】:PHP code not being executed (commented out) while using slim framework使用苗条框架时未执行 PHP 代码(已注释掉)
【发布时间】:2020-06-03 19:54:36
【问题描述】:

我正在使用超薄框架(路由等)并照亮数据库以在我的网页上输出数据,但它似乎无法正常工作,因为当我在浏览器(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;
    }

?>

^ 此代码在浏览器代码视图中显示为&lt;!-- code --&gt;

我是 php 框架的新手,因此不胜感激。

【问题讨论】:

    标签: php slim slim-3


    【解决方案1】:

    “视图”层不负责获取数据。相反,您应该使用 3. 参数传递所有数据以查看。

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

    【讨论】:

    • 使用我得到“传递给 Slim\Views\Twig::render() 的参数 3 必须是数组类型,给定对象,在 C:\xampp\htdocs\src\routes 中调用.php 第 20 行”错误。甚至使用一个简单的 似乎不起作用,因为它无论如何都会被注释掉。它与路由内部的渲染有什么关系吗?
    猜你喜欢
    • 1970-01-01
    • 2021-06-16
    • 2016-04-25
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2017-02-28
    • 2011-08-07
    相关资源
    最近更新 更多