Home  >  Article  >  Backend Development  >  YII2中,自己写的一个小组件,怎么添加到yii2框架中去,详情见图

YII2中,自己写的一个小组件,怎么添加到yii2框架中去,详情见图

WBOY
WBOYOriginal
2016-06-06 20:38:081079browse

YII2中,自己写的一个小组件,怎么添加到yii2框架中去,详情见图

以上是我的配置文件,报错是找不类。这个类在文件夹:项目\components\Stone.php

回复内容:

YII2中,自己写的一个小组件,怎么添加到yii2框架中去,详情见图

以上是我的配置文件,报错是找不类。这个类在文件夹:项目\components\Stone.php

写错了,先把他放components目录下

<code>php</code><code>'stone' => [
    'class' => 'app\components\Stone'
]
</code>

然后就可以用了yii::$app->stone

1.如果components 是在根目录下的话,在bootstrap中没有setAlias 吧。所以不能自动加载。

2.如果你的components 文件夹是在 common 这个文件夹下的话,那你的命名空间有问题。应该是 common\components\Stone

这样:

  • @app/config/web.php
<code>    return [
        'conmponent' => [
            'stone' => [
                'class' => 'app\components\Stone'
            ]
        ]
    ];
</code>
  • @app/compontents/Stone.php
<code>    namespace app\compontents;
    class Stone {} 
</code>

如果是adv,在common目录新建一个components文件夹,然后Stone.php放入其中~

<code>'stone' => [
    'class' => 'common\components\Stone'
]
</code>

当然了,命名空间得改成common\components

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn