Slim Framework - 使用数组和变量


slim framework - using arrays and variables

我的索引.php文件中有一个数组;

$teamnames = array('bristol_city', 'bristol_thunder', 'bristol_uni', 'exeter_city', 'exeter_uni', 'gloucester', 'horfield', 'taunton');

然后我有一个"控制器",我想将该数组传递给add-report.php视图;

$app->get('/report/add', function () use($app) {
    $app->render('add-report.php', array());
});

我尝试将$teamnames添加到array(),然后尝试对视图进行print_r,但我得到的只是一个错误;

Type: ErrorException
Code: 8
Message: Undefined variable: teamnames
File: /vagrant/ref-feedback/index.php
Line: 18

我是 Slim 的新手,根本找不到任何关于此的文档......

你可以试试这个:

$app->get('/report/add', function () use($app) {
    $teamnames = array('bristol_city', 'bristol_thunder', '...');
    $app->render('add-report.php', array('teamnames' => $teamnames));
});

数组将被提取到视图中,因此现在您可以在视图中使用 print_r($teamnames)