简体   繁体   English

在 YII2 中找不到类“Html”

[英]Class 'Html' not found in YII2

Finally I try YII2 (can't move on from yii1) in my simple website project.最后,我在我的简单网站项目中尝试了 YII2(无法从 yii1 继续)。 I've Fatal ERROR Class 'Html' not found in my views.我在我的视图中Class 'Html' not found致命错误Class 'Html' not found

Case 1. I put use yii\\helpers\\Html;案例 1. 我把 use yii\\helpers\\Html; in main (layout directory).在主(布局目录)中。 When I write Html::img('@web/logo.jpg') in index(view directory)I've got error 'html not found'.当我在 index(view directory) 中写入Html::img('@web/logo.jpg') ,出现错误'html not found'.

Case 2. I write Html::img('@web/logo.jpg') in main (layout directory) running well.案例2.我在main(布局目录)中编写Html::img('@web/logo.jpg') ) 运行良好。

case 3. I delete use yii\\helpers\\Html in main (layout directory).案例3.我删除了main(布局目录)中的use yii\\helpers\\Html and I write use yii\\helpers\\Html in index (view directory) running well, my image can show.并且我yii\\helpers\\Html in index运行良好的yii\\helpers\\Html in index (视图目录)中编写 use yii\\helpers\\Html in index ,我的图像可以显示。

so, what's my mistake.所以,我的错误是什么。 Why I can't Html in index.为什么我不能在 index.html 中使用 Html。

Every single file needs to use fully qualified namespaces or have them as use declarations.每个文件都需要使用完全限定的命名空间或将它们作为use声明。

So every file where you are using Html::... needs to get this, not only layout file.所以你使用Html::...每个文件都需要得到这个,而不仅仅是布局文件。

use使用

yii\helpers\Html;

HTML provides a set of static methods for generating commonly used HTML tags. HTML 提供了一组静态方法来生成常用的 HTML 标签。

Nearly all of the methods in this class allow setting additional html attributes for the html tags they generate.此类中的几乎所有方法都允许为它们生成的 html 标签设置额外的 html 属性。 You can specify, for example, class, style or id for an html element using the $options parameter.例如,您可以使用 $options 参数为 html 元素指定类、样式或 id。 See the documentation of the tag() method for more details.有关更多详细信息,请参阅tag()方法的文档。

在此更改重置您的 Web 服务器后,取消注释 short_open_tag 并设置值 ON,也使用文件顶部的类元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM