2015-12-08 100 views
0

我在yii2中有以下代碼,但驗證碼圖像不顯示!
控制器:
yii2 captcha圖像不顯示

public function actions() { 
    return [ 
     'captcha' => [ 
      'class' => 'yii\captcha\CaptchaAction', 
      'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, 
      'foreColor' => 0xF9AF21, 
      'maxLength' => 5, 
      'minLength' => 3, 
      'padding' => 5, 
      'offset' => 1, 
      'transparent' => true, 
      'height' => 40 
     ], 
     'error' => [ 
      'class' => 'yii\web\ErrorAction', 
     ], 
    ]; 
} 

模型:(規則)

['verifyCode', 'captcha',], 

視圖:

$form->field($model, 'verifyCode')->widget(Captcha::className()]) 

the login page

回答

5

SiteController尋找行爲()功能,它可能看起來像在下面的例子。

public function behaviors() 
{ 
    return [ 
     'access' => [ 
      'class' => AccessControl::className(), 
      'only' => ['logout', 'signup'], 
      'rules' => [ 
       [ 
        'actions' => ['signup'], 
        'allow' => true, 
        'roles' => ['?'], 
       ], 
       [ 
        'actions' => ['logout'], 
        'allow' => true, 
        'roles' => ['@'], 
       ], 
      ], 
     ], 
    ]; 
} 

你不會看到你的行爲()功能不會被指定'only'行動,這樣驗證碼圖片,如果:'only' => ['logout', 'signup'],。該行表示僅將訪問規則應用於此操作。如果您不想將規則添加到特定操作,則可以將'captcha'操作添加到您的規則中,如下面的示例所示。

public function behaviors() 
{ 
    return [ 
     'access' => [ 
      'class' => AccessControl::className(), 
      'rules' => [ 
       [ 
        'actions' => ['signup', 'captcha'], 
        'allow' => true, 
        'roles' => ['?'], 
       ], 
       [ 
        'actions' => ['logout'], 
        'allow' => true, 
        'roles' => ['@'], 
       ], 
      ], 
     ], 
    ]; 
} 
+0

好的!這真是很好的解釋。 –

0

控制器:

public function actions() 
{ 
    return [ 
     'error' => [ 
      'class' => 'yii\web\ErrorAction', 
     ], 
     'captcha' => [ 
      'class' => 'yii\captcha\CaptchaAction', 
      'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, 
      'foreColor' => 115006, 
      'backColor' => 333333, 
      'height' => 30, 
      'maxLength' => 4, 
      'minLength' => 4, 
      'offset' => 2, 
      'testLimit' => 1, 
     ], 
    ]; 
} 

型號:

public function rules() 
{ 
    return [    
     ['verifyCode', 'captcha'],   
    ]; 
} 

查看:

use yii\captcha\Captcha; 

<?= $form->field($model, 'verifyCode')->widget(Captcha::classname()) ?> 
+0

我的問題在哪裏? – mohsen

+0

您的模特展示 – vishuB

+0

是否存在問題! – mohsen

-1

刪除站點控制器此功能和解決的問題:

public function behaviors() 
{ 
    return [ 
     'access' => [ 
      'class' => AccessControl::className(), 
      'rules' => [ 
       [ 
        'actions' => ['login', 'error'], 
        'allow' => true, 
       ], 
       [ 
        'actions' => ['logout', 'index'], 
        'allow' => true, 
        'roles' => ['@'], 
       ], 
      ], 
     ], 
     'verbs' => [ 
      'class' => VerbFilter::className(), 
      'actions' => [ 
       'logout' => ['post'], 
      ], 
     ], 
    ]; 
}