php - 请问YII2的自定义组件应该放在什么位置,用什么文件名

 fo切為祢 发布于 2022-11-18 06:13

//定义是MyController.php
namespace appcomponents;
use yiibaseWidget;
use yiihelpersHtml;

class HelloWidget extends Widget
{

public $message;

public function init()
{
    parent::init();
    if ($this->message === null) {
        $this->message = 'Hello World';
    }
}

public function run()
{
    return Html::encode($this->message);
}

}

//然后调用是在view中
use appcomponentsHelloWidget;
?>
'Good morning']) ?>

//结果报错找不到组件
Class 'appcomponentsHelloWidget' not found

请问我定义是不是错了呀?

1 个回答
  • 命名空间的问题
    很少用app开始的明明空间,一般都是common/components;或者根据模块来,frontend backend

    2022-11-18 07:50 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有