1

how do you use html file views in Silex.

I am trying to use silex with some custom php code where the controller method includes the views files.

Here is what I have: in my index.php

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

And here is the show method of my controller:

    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();  

Is there a way to make this work?

I do not want to move the logic of which views to show from the controller to index.php. And also I do not want to use twig for now.

And here is the error that I get:

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

thanks

guntbert
  • 536
  • 6
  • 19
Adam Tong
  • 571
  • 1
  • 4
  • 16

2 Answers2

0

From your error:

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

One can safely assume that your problem is that one of your includes is failing. From the PHP manual regarding the include:

include returns FALSE on failure and raises a warning

So probably you're trying to include a non existent file or maybe one of those is generating an error.

On the development environment, you should make sure that your PHP errors are shown (take a look at this question)

Community
  • 1
  • 1
mTorres
  • 3,590
  • 2
  • 25
  • 36
0

The error you're getting is due to the fact that ob_end_clean returns a boolean (success or failure). The function you're probably looking for is 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();  
Francis Eytan Dortort
  • 1,407
  • 14
  • 20