天天看点

THINKPHP 3.2.3 widget 调用模板无显示

问题描述:{:W(‘Menu/showLeftMenu’)} 无法加载Menu下面的‘menu.html’

1、这里是 MenuWidget 的代码片段:

<?php
namespace Home\Controller;    
use Think\Controller;

class MenuWidget extends Controller{
   public function showLeftMenu(){
        $this->display('Menu:menu');
    }
}
?>
           

2、menu.html 路径:“View/Menu/menu.html”

3、menu.html 代码:

4、主调页面路径:“View/Index/index.html”

IndexController.class.php 代码:

<?php
    namespace Home\Controller;
    use Think\Controller;

    class IndexController extends  Controller{
        $this->display();
    }
?>
           

index.html 代码:

<html>
    <head>
        <title>hello world!</title>
    </head>
    <body>
        {:W('Menu/showLeftMenu')}
    </body>
</html>
           

结果调用失败,无显示

错误分析:

其实这里的代码没有什么大的问题,主要错误的原因是:我把Widget错误的当做Controller 放在了Controller目录里面。

解决方式:

1、将Menu放到与Controller同级的Widget目录中

2、将MenuWidget 里面的工作空间代码改为:namespace Home\Widget;目录即可。

<?php
namespace Home\Widget;    
use Think\Controller;

class MenuWidget extends Controller{
   public function showLeftMenu(){
        $this->display('Menu:menu');
    }
}
?>
           

PS:这是我用markdown在CSDN里面写的第一个总结,后期我会把我遇到的一些困难和解决方式放到这里面,就作为我的成长记录吧,也希望对遇到同样问题的小伙伴有所帮助;