您的位置:首页 > 其它

认领你的博客

2010-05-28 16:46 155 查看
事先声明一下,我几乎是跟着书上做的,不过书上的是phpweb20,我的目录是pyyweb,其他都一样。
在学到 的时候,出现了这种错误:
Zend_View_Exception: script 'news/index.phtml' not found in path (/xampp/htdocs/pyyweb/include/views\scripts/) in M:\xampp\htdocs\pyyweb\include\Zend\View\Abstract.php on line 980
路径:pyyweb/include/Controllers/NewsController.php 的内容为:
 
<?PHP
class NewsController extends Zend_Controller_Action//CustomControllerAction
{
public function indexAction()
{
require_once('Smarty/Smarty.class.php');

$articles = array('News Article 1',
'Another News Article',
'Even More News');

$smarty = new Smarty();
$smarty->template_dir = $config->paths->templates;
$smarty->compile_dir = $config->paths->data . '/tmp/templates_c';
$smarty->assign('news', 'hhhhh');
$smarty->display('news\index.tpl');
}
}
?>


这里的代码暂时找不出什么问题,于是只好按照它的错误提示,既然它说在 /xampp/htdocs/pyyweb/include/views\scripts/ 中没有news/index.phtml ,那就新建给它好了,暂时是这样吧:
路径:pyyweb/include/views/scripts/news/index.phtml 的内容为:
 
<h1>index.phtml in: pyyweb/include/views/scripts/news/</h1>


结果一刷新,又有新的错误了:
 

SmartyException: Unable to load template file 'news\index.tpl' in M:\xampp\htdocs\pyyweb\include\Smarty\sysplugins\smarty_internal_templatebase.php on line 127

又说没有文件?那好,再建一个不就行了!
路径:/pyyweb/templates/news/index.tpl 的内容为:
 
<h1>index.tpl in file: /pyyweb/templates/news/index.tpl</h1>


再刷新,惊喜地发现 ---- 还是那个错误!?
 

于是接下来就是漫长的高试过程,echo 和 Xdebug 双管齐下,终于在悠悠暗无天日的调试岁月中发现了一丝曙光……
突然发现,这个文件中的 pyyweb/include/Controllers/NewsController.php 某个变量有点奇怪:$config。于是对比了一下 index.php 中的用法,果然是因为 $config 没有被预定义,只要在使用 $config 之前加一句 $config = Zend_Registry::get('config'); 就行了,修改之后的 index.php 内容如下。
路径:pyyweb/include/Controllers/NewsController.php 的内容为:
 
<?PHP
class NewsController extends CustomControllerAction
{
public function indexAction()
{
require_once('Smarty/Smarty.class.php');

$articles = array('News Article 1',
'Another News Article',
'Even More News');

$smarty = new Smarty();
$config = Zend_Registry::get('config');
$smarty->template_dir = $config->paths->templates;
$smarty->compile_dir = $config->paths->data . '/tmp/templates_c';
$smarty->assign('news', 'hhhhhhh');
$smarty->display('news\index.tpl');
}
}
?>


这样一下,再次刷新之后,终于正确显示了:
 

 
index.phtml in: pyyweb/include/views/scripts/news/

index.tpl in file: /pyyweb/templates/news/index.tpl


问题一,为什么显示了 index.phtml 和 index.tpl 两个文件的内容呢?

 

Zend_Controller_Front 在收到 http://pyyweb/news 的要求时,会先按照内部预定义的方式显示 "某个目录" 下的 “某个文件”。暂时我还不知道 “某个目录” 和 “某个文件” 是在何时、何地定义的,但很显然访问 news 动作时的默认目录是: pyyweb/include/views/scripts/news/  而默认文件为:index.phtml 。
只有当上面的步骤完成之后,页面显示了 index.phtml 中的内容之后,才会运行到 NewsController 中的 indexAction() 方法。而 indexAction() 又要求显示 index.tpl 的内容,所以页面上才会显示两个文件的内容。
 

问题二,为什么要用 Templater 类?

很显然,一个 news 控制器不只有 index 动作,还可能会有 photos 动作、views 动作等等,假如有几百个动作,那岂不是要将 NewsController 中 indexAction() 方法里的内容重复几百遍?而且每个动作都要有不同的 $smarty->display('news\index.tpl')。这显然是不合适的。而且如果用默认的视图显示类,如上所述,会默认显示 index.phtml 的内容,不成功则报错。用 Templater 类可以重写这种默认的规则。
 

问题三,用了Templater类之后Smarty元语言报错?

在如书上所示增加了 Templater 类之后,为了验证 Smarty 元语言的作用,模板文件更新如下:
 

路径:/pyyweb/templates/news/index.tpl 的内容为:
 
<h1>index.tpl in file: /pyyweb/templates/news/index.tpl</h1>{$news}

刷新,结果index.tpl 的内容显示了两遍,第一遍的 {$news} 内容没有显示出来,而且报错了。第二遍的显示正确。其中错误如下:
 

Notice: Undefined index: news in M:\xampp\htdocs\pyyweb\data\tmp\templates_c\7875d1e9dddb74e56a7cb6b41ba870b80dea5f2a.file.index.tpl.php on line 27

由于$news 是在 NewsController 的 indexAction() 中被赋值的。而 Templater 在默认的显示index.tpl的内容时,并未对 $news 赋值,所以报错。当执行完indexAction() 之后,$news已经被赋值了,所以正确显示。这个问题现在我还没有解决办法,接着看书,希望有解吧……
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: