2015-10-18 82 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