尝试使用Redactor获得yii2 Advanced工作,文本区域可以显示但图像插入失败。总是得到错误POST和GET错误500。 我已经使用Yii2 web上的UploadedFile教程进行了测试,图像可以成功上传并显示在目录中,但是redactor无法正常工作。
这是我的文章控制器:
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]);
}
这是我的模特:
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]],
];
}
}
并查看:
<?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()?>
我打开底部的yii2调试并检查错误,发现错误一直说:
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}
然后我去Redactor Module检查配置:
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'];
似乎所有内容都设置正确,在/ web / uploads上,我已成功上传了之前UploadedFile测试的图像。 此外,尝试多次手动分配索引“ClientOptions”上的目录,问题仍然存在。