Search code examples
yii2redactor

yii2 advanced redactor image upload fail with error 'Invalid config $uploadDir'


Trying to get yii2 Advanced work with Redactor, the text-area can show but image insert fails. Always get error POST and GET Error 500. I have tested with the UploadedFile tutorial on the Yii2 web, image can successfully uploaded and display on the directory, but redactor cannot work.

Here is my article controller:

namespace backend\controllers;

use yii\web\Controller;
use common\models\Article;
use Yii;

class ArticleController extends Controller{

    public $layout='empty';

    public function actionIndex(){
        $model = new Article;

        if($model->load(Yii::$app->request->post()) && $model->validate()){
            echo  "good operation";
        }
        return $this->render('index', ['model'=>$model]);
    }

Here is my model:

namespace common\models;

use yii\db\ActiveRecord;
use Yii;

class Article extends ActiveRecord{

    //const SCENARIO_LOGIN = 'login';
    //const SCENARIO_REGISTER = 'register';

    public static function tableName(){
        return '{{%article}}';
    }

    public function rules(){
        return [
            [['title','description','content'],'required','message'=>'{attribute} required'],
            ['title','unique','message'=>'sorry,{value} is taken'],
            ['status','in','range'=>[0,1]],
            ];
    }


}

And view:

<?php
use yii\helpers\Url;
use yii\widgets\Breadcrumbs;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>

<?=Breadcrumbs::widget([
    'homeLink'=> ['label'=>'Home'],
    'links'=> [
            ['label'=>'Add New Article'],
        ],
])?>

    <?php
    $form = ActiveForm::begin([
            'id'=>'article',
            'options'=>['class'=>'wenzhang'],
        ]);
    ?>
    <?=$form->field($model, 'title')?>
    <?=$form->field($model,'description')?>
    <?=$form->field($model,'content')->widget(\yii\redactor\widgets\Redactor::className(),
        [
            'clientOptions'=>[
                    'imageManagerJson' =>['/redactor/upload/image-json'],
                    'lang'=>'zh_cn',
                    'plugins'=>['clips','fontcolor','imagemanager'],
                    'imageUpload' => \yii\helpers\Url::to(['/redactor/upload/image']),
                ],    
        ]
        )?>

    <button>Submit</button>
    <?php ActiveForm::end()?>

I open the yii2 debug at bottom and check the error, find that the error keeps saying:

exception 'yii\base\InvalidConfigException' with message 'Invalid config $uploadDir' in /home/ubuntu/workspace/yii2AdvancedTest/vendor/yiidoc/yii2-redactor/RedactorModule.php:51
Stack trace:
#0 /home/ubuntu/workspace/yii2AdvancedTest/vendor/yiidoc/yii2-redactor/actions/ImageManagerJsonAction.php(33): yii\redactor\RedactorModule->getSaveDir()
#1 [internal function]: yii\redactor\actions\ImageManagerJsonAction->run()
#2 /home/ubuntu/workspace/yii2AdvancedTest/vendor/yiisoft/yii2/base/Action.php(92): call_user_func_array(Array, Array)
#3 /home/ubuntu/workspace/yii2AdvancedTest/vendor/yiisoft/yii2/base/Controller.php(154): yii\base\Action->runWithParams(Array)
#4 /home/ubuntu/workspace/yii2AdvancedTest/vendor/yiisoft/yii2/base/Module.php(454): yii\base\Controller->runAction('image-json', Array)
#5 /home/ubuntu/workspace/yii2AdvancedTest/vendor/yiisoft/yii2/web/Application.php(100): yii\base\Module->runAction('redactor/upload...', Array)
#6 /home/ubuntu/workspace/yii2AdvancedTest/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#7 /home/ubuntu/workspace/yii2AdvancedTest/backend/web/index.php(18): yii\base\Application->run()
#8 {main}

Then I go to Redactor Module to check the config:

   public $controllerNamespace = 'yii\redactor\controllers';
    public $defaultRoute = 'upload';
    public $uploadDir = '@webroot/uploads'; 
    public $uploadUrl = '@web/uploads';
    public $imageUploadRoute = ['/redactor/upload/image'];
    public $fileUploadRoute = ['/redactor/upload/file'];
    public $imageManagerJsonRoute = ['/redactor/upload/image-json'];
    public $fileManagerJsonRoute = ['/redactor/upload/file-json'];
    public $imageAllowExtensions = ['jpg', 'png', 'gif', 'bmp', 'svg'];

Everything seems to be set correctly, and on the /web/uploads, I have successfully uploaded images on the prior UploadedFile test. Also, tried to manually assign the directory on the index "ClientOptions" several times, problem still resists.


Solution

  • Update version of your widget, as you can see here Github, developer do some change to prevent this error