如何在yii2高级模板中使用自定义小部件

时间:2016-06-02 05:42:35

标签: yii2 yii2-advanced-app

我制作简单的自定义Hellowidget。

HelloWidget类和文件放在“组件”目录和组件目录中,放在“应用程序的根目录”中

`     public $ message;

#import "Foundation/Foundation.h"

// Extend NSString to initialize from an array of NSString hex values
@interface NSString (HexConversion)

+ (NSString *) stringFromHexArray:(NSArray *)array;

@end

@implementation NSString (HexConversion)

// Assumes NSArray is filled with NSStrings representing hex values, could easily be changed
+ (NSString *) stringFromHexArray:(NSArray *)array {
    NSMutableString *mString = [[NSMutableString alloc] init];
    for(NSString *hexItem in array) {
        int value = 0;
        sscanf([hexItem cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
        [mString appendFormat:@"%c", (char)value];
    }
    return mString;
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        // Your test hex array
        NSArray *testArray = @[@"01", @"ca", @"04", @"3a", @"7b", @"22", @"70", @"61", @"73", @"73", @"22", @"3a", @"22", @"6b", @"61", @"6e", @"6f", @"6b", @"22", @"2c", @"22", @"73", @"73", @"69", @"64", @"22", @"3a", @"22", @"6f", @"70", @"65", @"6e", @"57", @"72", @"74", @"41", @"70", @"6c", @"31", @"32", @"38", @"22", @"2c", @"22", @"75", @"73", @"65", @"72", @"22", @"3a", @"22", @"6b", @"61", @"6e", @"6f", @"6b", @"22", @"7d"];

        // Convert to string!
        NSString *string = [NSString stringFromHexArray:testArray];

        // Verify conversion, prints Ê:{"pass":"kanok","ssid":"openWrtApl128","user":"kanok"}
        NSLog(@"%@", string); 
    }
    return 0;
}

`

此窗口小部件在视图中调用

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

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

所以我收到错误“类'app \ components \ HelloWidget'找不到”命名空间添加仍然收到错误

关于该小部件的任何想法

1 个答案:

答案 0 :(得分:0)

您好我找到了解决方案

如果您使用的是yii2高级模板,那么如果您将类找不到错误,组件就不能放入“根目录”

所以解决方案就是如果你在“前端”上工作,那么组件放在那个目录中然后使用自定义小部件它会起作用;)