Sta*_*ill 4 php zend-framework
我正在使用Zend框架书,并且在我更改默认视图位置的示例上停留了好几天.方法如下:
$this->view->setScriptPath("/views/");
$this->render("news");
Run Code Online (Sandbox Code Playgroud)
我将我的news.phtml文件放在views目录中(而不是默认值,views/scripts/artist但我得到的只是一条消息,说明找不到页面.我已尝试过很多来自网络的方法,比如打字
$this->view->setScriptPath("/application/views/");
Run Code Online (Sandbox Code Playgroud)
要么
$this->view->setScriptPath(APPLICATION_PATH."/views/");
Run Code Online (Sandbox Code Playgroud)
但都没有用.
有人可以赐教吗?
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');
}
Run Code Online (Sandbox Code Playgroud)
我把news.phtml在views目录中.我输入的URL是http://localhost:10088/loudbite/artist/news.的artistController是在控制器中的文件夹.仍然无法正常工作.怎么了?
使用setScriptPath()或时addScriptPath,需要指定目录的绝对路径或相对于当前目录的路径.为了便于携带,最好使用绝对路径.
在呼叫时,Zend_View::render()您必须传递脚本名称,包括其扩展名.
根据您的上一个示例,尝试这样的事情:
$view->setScriptPath(APPLICATION_PATH . '/views/');
$html = $view->render('news.phtml');
Run Code Online (Sandbox Code Playgroud)
只要确保路径正确.我的例子假设你views在你的application文件夹中.
编辑: 如果您在控制器中并且想要使用其他视图脚本,请改用View Renderer帮助程序:
$this->_helper->viewRenderer->setRender('news');
Run Code Online (Sandbox Code Playgroud)
这告诉视图渲染器查找视图脚本news.phtml而不是您的操作名称.但是,它仍然会查找views/scripts/controller/news.phtml.因此,您还需要进行以下更改:
// set view script path to the base of the views folder
$this->view->setScriptPath(APPLICATION_PATH . '/views/');
$this->_helper
->viewRenderer
->setNeverController(true) // do not render into controller subdirectories
->setRender('news'); // set render script to news.phtml
Run Code Online (Sandbox Code Playgroud)
当你使用Zend Application时,它会自己进行视图渲染,所以除非你试图直接渲染html,否则你不应该自己使用Zend_View.控制器操作方法完成后,Zend Application将自动呈现视图脚本并尝试将其与任何布局一起输出到浏览器.如果您确实创建了自己的Zend_View,则需要在操作完成之前终止请求,以免呈现任何其他内容.还有一些方法可以禁用布局或视图渲染作为替代方法.