Slim - 在包含的文件中使用路由变量

Slim - Use variables of a route inside an included file

我正在使用 Slim 3 构建应用程序。

在某个路由中,我通过include_once包含了模板的header(slim/php-view 2.2),有一些我发送到我需要在 header 中使用的模板的变量。

有办法吗?

我的路线:

$app->get('/', function ( $request,  $response) {
    // some code here...
    $somedata = ' this is just a test';

    return $this->renderer->render($response, "/home.phtml",[
        'somedata' => $somedata,
    ]);
})

目标模板(home.phtml):

<?php include_once('myheader.php'); ?>

    <h1>This is my template</h1>
    <p> I need to use this variable <?=$somedata?> 
    in the included myheader.php file</p>

我的错。阅读 the docs 我发现我可以使用 模板变量 实现我想要的,像这样:

我的路线:

$templateVariables = [
    "title" => "Title"
];
$phpView = new PhpRenderer("./path/to/templates", $templateVariables);

我的包含文件(myheader.php):

<?php echo $title ?>