qodeninja
qodeninja

Reputation: 11266

How do you include PHP files in the view file of a Zend/MVC?

Maybe I dont understand the MVC convention well enough, but I'm trying to include a file to the index.phtml view for the main Index Controller, and it keeps giving me an Application Error. I have no idea what this error is or why its not working. But I'm using a standard include_once(...) in the view.

Is this even allowed?

Upvotes: 0

Views: 4053

Answers (5)

Syed Ahmed
Syed Ahmed

Reputation: 1645

last time this works fine for me. You can try this:

    <?php echo $this->partial('common/left_menu.phtml'); ?> 

Upvotes: 0

George
George

Reputation: 31

You can use Zend View Helpers for this purpose

Upvotes: 0

Roger Halliburton
Roger Halliburton

Reputation: 2021

A view in Zend is still just a php file. If you are getting errors in a view using include_once(), they are probably because the file you want can't be found in your include path. Trying dumping get_include_path() into the view and you will see what directories PHP is searching to find your included file.

As an alternative to include_once, you could use

<? echo $this->render('{module}/{action}.phtml') ?>

to pull in the file.

Upvotes: 3

mingos
mingos

Reputation: 24502

The view is only the HTML that will be rendered. It's the very last thing that is processed. The controller is called first, then it calls whatever models are needed within. After passing all the data to the view, the view's HTML is rendered.

In short: whatever you include in the view, the controller isn't aware of it. You need to run your PHP includes earlier in the code. If you do it in the controller, it should work OK, I suppose (not tested, so I don't guarantee anything).

Upvotes: 2

zerkms
zerkms

Reputation: 254916

There are partial views for such purpose

http://framework.zend.com/manual/en/zend.view.helpers.html (ctrl+f Partial Helper)

Upvotes: 2

Related Questions