【问题标题】:call html (php files) in silex在silex中调用html(php文件)
【发布时间】:2015-10-18 18:22:57
【问题描述】:

如何在 Silex 中使用 html 文件视图。

我正在尝试将 silex 与一些自定义 php 代码一起使用,其中控制器方法包括视图文件。

这是我所拥有的:在我的 index.php 中

$app->get('/', function (Request $request) {    
    $homeController = new HomeController($request);
    $output = $homeController->show($request);
     return new Response($output);
});
$app->run();

这是我的控制器的显示方法:

    ob_start();
    include "view/start.html.php";
    include "view/header.html.php";
    include "view/contact.html.php";
    include "view/footer.html.php";
    include "view/end.html.php";
    return ob_end_clean();  

有没有办法让它工作?

我不想将显示哪些视图的逻辑从控制器移到 index.php。而且我暂时不想使用twig。

这是我得到的错误:

UnexpectedValueException in Response.php line 403:
The Response content must be a string or object implementing __toString(), "boolean" given.

谢谢

【问题讨论】:

  • return new Response($output);?
  • 什么不起作用?有什么问题?我不明白。
  • @AI.G 我添加了收到的错误消息。
  • @u_mulder 我不明白你的评论。这是一个问题吗?

标签: php silex


【解决方案1】:

您遇到的错误是由于ob_end_clean 返回一个布尔值(成功或失败)。您可能正在寻找的功能是ob_get_clean。

ob_start();
include "view/start.html.php";
include "view/header.html.php";
include "view/contact.html.php";
include "view/footer.html.php";
include "view/end.html.php";
return ob_get_clean();  

【讨论】:

    【解决方案2】:

    根据您的错误:

    响应内容必须是字符串或实现__toString()的对象,“boolean”给定。

    可以放心地假设您的问题是您的一个包含失败。来自关于include的PHP手册:

    include 在失败时返回 FALSE 并引发警告

    因此,您可能正在尝试包含一个不存在的文件,或者其中一个文件正在生成错误。

    在开发环境中,您应该确保显示您的 PHP 错误(查看this question)

    【讨论】:

      猜你喜欢
      • 2014-07-19
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多