I am working through a Zend framework book and is stuck for days on the example where I change the default view location. The method is as listed:
$this->view->setScriptPath("/views/");
$this->render("news");
I place my news.phtml file in the views directory(rather than the default views/scripts/artist but all I got was a message that says the page is not found. I have tried many methods from the web, such as typing
$this->view->setScriptPath("/application/views/");
or
$this->view->setScriptPath(APPLICATION_PATH."/views/");
but all are not working.
Can someone please enlighten me?
To improve the clarity, I kind of suspect it got to do with my machine setup. I am running on Mac 10.7 and I have activated the built-in PHP and Apache. Since Zend provides it’s own stack, will there be any clash in the setting files such as php.ini?
More edits:
I am putting down the whole method:
public function newsAction()
{
//Check if the user is logged in
//Get the user's id
//
//Get the artists
$artists = array("Thievery Corporation",
"The Eagles",
"Elton John");
//Set the view vairables
$this->_helper->viewRenderer->setRender('news');
$this->view->setScriptPath(APPLICATION_PATH.'/views/');
$this->_helper->viewRenderer->setNeverController(true)->setRender('news');
}
I placed the news.phtml in the views directory. The URL I type is http://localhost:10088/loudbite/artist/news. The artistController is in the controllers folder. Still doesn’t work.
What is wrong?
When using
setScriptPath()oraddScriptPath, you need to specify the absolute path to the directory, or a path relative to your current directory. For portability, using an absolute path is best.When calling
Zend_View::render()you must pass the script name including its extension.Based on your last example, try something like this:
Just make sure to get the path correct. My example assumes you have
viewsin yourapplicationfolder.EDIT:
If you are in a controller and you want to use a different view script, use the View Renderer helper instead:
This tells the view renderer to look for a view script
news.phtmlinstead of the name of your action. However, it still looks inviews/scripts/controller/for news.phtml. Therefore you need the following changes as well:When you are using Zend Application, it does its own view rendering, so unless you are trying to render html for use directly, you shouldn’t use Zend_View yourself. After your controller action method completes, Zend Application is going to automatically render a view script and attempt to output it, along with any layout to the browser. If you did create your own Zend_View, you would need to terminate the request before the action finishes, as not to render any other content. There are also ways to disable the layout or view rendering as an alternative.