2014-10-19 49 views
0

我使用gii在yii2.0中生成擴展名,這裏是代碼如何在yii2中使用自定義擴展/小部件?

namespace ms \ editor;

/** 
* This is just an example. 
*/ 
class AutoloadExample extends \yii\base\Widget 
{ 
    public function run() 
    { 
     return "Hello!"; 
    } 
} 
時,我想在我的視圖文件使用它

use ms\editor\AutoloadExample; 
... 
<?= AutoloadExample::widget();?> 

我用yii2 baisic模板,我把「MS」文件夾中的「供應商」文件夾中,但它只是告訴我沒有找到類ms \ editor \ AutoloadExample,我應該如何讓yii2找到類?yii1.1中是否有類似「components」或「extension」文件夾的東西? 你能幫助我嗎?

回答

0

您的小部件應繼承CWidget類並將小部件放置在組件中。

public class AutoloadExample extends CWidget 
{ 
    public function run() { } 
} 

然後,您可以在您的視圖中像這樣運行小部件;

$this->widget('application.components.AutoloadExample', array('your variables')); 
+0

在yii 2中它不起作用,它只能在yii 1中工作,謝謝你們一樣。 – penn 2014-10-19 13:20:29

0

你會試試嗎? 例如創建一個文件 「應用程序/前端/小工具/」

文件名:Deneme.php

<?php 

namespace frontend\widgets; 

class Deneme 
{ 

    static function yazdir() { 

    echo 'asd'; 

    } 

} 

使用;

use frontend\widgets\Deneme; 

Deneme::yazdir(); 
+0

我試過這個,但它也說「找不到類」。 – penn 2014-10-20 06:28:55

+0

你的yii 2是什麼類型?高級還是基本? – Serhat 2014-10-20 11:22:44

相關問題