2017-03-16 110 views
1

當運行通過終端的命令控制器,所述 Yii::getAlias('@webroot')不起作用=/Yii2 - 命令控制器不工作getalias根目錄

命令(QueuePushNotificationController.php):

<?php 
    namespace app\commands; 

    use Yii; 
    use yii\console\Controller; 

    class QueuePushNotificationController extends Controller 
    { 
     public function actionIndex() 
     { 
      echo Yii::getAlias('@webroot'); 
     } 
    } 

經由終端執行:

./yii queue-push-notification 

回報:

Exception 'yii\base\InvalidParamException' with message 'Invalid path alias: @webroot' 

不適用於應用程序嗎?

謝謝!

回答

3
在yii2命令undefind(Web根目錄或網頁)的別名,網頁的index.php警予\網絡\ Application.php defind方法

protected function bootstrap() 
{ 
     $request = $this->getRequest(); 
     Yii::setAlias('@webroot', dirname($request->getScriptFile())); 
     Yii::setAlias('@web', $request->getBaseUrl()); 

     parent::bootstrap(); 
} 

,你可以看到警予\控制檯\應用不包括自舉()方法。 你可以很好地解決這個問題,

public function init() 
{ 
     Yii::setAlias('@webroot', Specific path); 
     Yii::setAlias('@web', Specific path); 
     parent::init(); // TODO: Change the autogenerated stub 
}