2015-10-18 83 views
1

如何在Silex中使用html文件视图。在silex中调用html(php文件)

我想用一些自定义的php代码,其中控制器方法包括视图文件使用silex。

以下是我有:在我的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。而且我现在也不想用树枝。

这里是错误,我得到:

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

感谢

+0

'return new Response($ output);'? –

+0

什么不起作用?有什么问题?我无法理解。 –

+0

@ AI.G我添加了我得到的错误信息。 –

回答

0

你得到的错误是由于这样的事实: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(); 
+0

谢谢弗朗西斯! –

0

从你的错误:

The Response content must be a string or object implementing __toString(), "boolean" given.

我们可以有把握地认为你的问题是,你包括一个是失败。从有关include PHP手册:

include returns FALSE on failure and raises a warning

所以也许你想包括一个不存在的文件或也许那些之一是产生错误。

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