I have a situation where I intended to step away from the object oriented outline and just do a raw php file include into one of my controllers.
The reason it that I want to include an unknown number of extra form fields into an entity’s form twig file (these form fields are defined in a separate file that I want to include).
My intent is the to – in the submit action – loop over these fields (say data_1, data_2 etc.) and serialize them and store them in the entity property “data”. The view that will output the result of this entity will then also – based on the template type – know how many and what extra fields to expect and output them in a correct manner, thanks to a similar design in including a view-template for each template type.
Now, when doing this:
//All template form files store all their html in $output
include '/'.$templatePath.'/form.php';
return $output;
I get a symfony error (not a php error!) saying
Warning: include(/main/1/form.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in /var/www/biztv/src/BizTV/ContentManagementBundle/Controller/DefaultController.php line 136
So is this just saying I mis-traced the path to what I am trying to include, or is it, like I suspect, a forbidden action with symfony2? Any other method to accomplish what I am trying here?
Looking at just your code, you have a slash at the beginning of your path, that will point to the root filesystem …
Use a correct relative path (assuming your current file is Controller/xxx.php and your form is in Forms/form.php):
But you should use output buffering to capture the result of the inclusion of the .php file, and then pass the content to your twig template. Otherwise it may disturb what headers should be sent etc.
In you controller:
And then in your twig template: