在laravel中使用WangEditor和多图上传
时间:2023-03-30 01:47:30
PHP
在laravel中使用WangEditor和多图上传1.创建一个项目,安装需要的安装包1.1创建一个项目composercreate-projectlaravel/laravel=5.3blog_wangeditor--prefer-dist1.2创建数据库和配置文件vim.env1.3安装中文包composerrequire"caouecs/laravel-lang:~3.0"安装完成后,将语言包移动到相应位置即可。语言包默认位置为resources/langcp-avendor/caouecs/laravel-lang/src/zh-CNresources/lang修改config/app.php将本地值改为zh-CN1.4安装laravel-admincomposer安装composerrequireencore/laravel-admin"1.3.*"inconfig/app.phpjoinServiceProvider:Encore\Admin\Providers\AdminServiceProvider::class发布资源phpartisanvendor:publish--tag=laravel-admininstallphpartisanadmin:install1.5快速生成前端登录注册模块phpartisanmake:auth执行Migratephpartisanmigrate2。快速生成文章管理2.1创建迁移表phpartisanmake:migrationcreate_posts_table--create=posts2.2修改迁移表database/2018_01_14_191442_create_posts_table.phppublicfunctionup(){Schema::create('posts',function(Blueprint$table){$table->increments('id');$table->text('title');$table->text('副标题');$table->text('封面');$table->enum('type',['0','1','2','3','4','5','9'])->default('0');$table->text('内容');$表->时间戳();});}2.3执行迁移phpartisanmigrate2.4创建模型和控制器模型phpartisanmake:modelApp\\Models\\Postcontrollerphpartisanadmin:makePostController--model=App\\Models\\Post2.5新建文章后台路由管理/路线。php$router->resource('/post','PostController');2.6修改app/Admin/PostController.php中form和grid的两个方法protectedfunctiongrid(){returnAdmin::grid(Post::class,function(Grid$grid){$grid->id('ID')->sortable();$grid->title('title');$grid->subtitle('subtitle');$grid->type('type')->options(['0'=>'php','1'=>'laravel','2'=>'javascript','3'=>'python','4'=>'golang','5'=>'linux','9'=>'其他']);$grid->cover('cover')->image('/uploads',100,100);$grid->content('content')->limit(100);$grid->created_at('创建时间');$grid->updated_at('修改时间');});}protectedfunctionform(){returnAdmin::form(Post::class,function(Form$form){$form->display('id','ID');$form->text('title','title');$form->textarea('subtitle','subtitle')->rows(3);$form->select('type','type')->options(['0'=>'php','1'=>'laravel','2'=>'javascript','3'=>'python','4'=>'golang','5'=>'linux','9'=>'其他']);$form->image('cover','cover');$form->editor('content','content');$form->display('created_at','创建时间');$form->display('updated_at','修改时间');});}3。集成WangEditoreditor3.1移除现有组件,修改app/Admin/bootstrap.phpformatName($this->column);$this->script=<<id}');editor.customConfig.uploadFileName='mypic[]';editor.customConfig.uploadImgHeaders={'X-CSRF-TOKEN':$('input[name="_token"]').val()}editor.customConfig.zIndex=0;//上传路径editor.customConfig.uploadImgServer='/uploadFile';editor.customConfig.onchange=function(html){$('input[name=$name]').val(html);}editor.customConfig.uploadImgHooks={customInsert:function(insertImg,result,editor){如果(typeof(result.length)!="undefined"){for(vari=0;i<=result.length-1;i++){varj=i;varurl=result[i].newFileName;插入图片(网址);}toastr.success(result[j]['info']);}switch(result['ResultData']){case6:toastr.error("Upto4imagescanbeuploaded");休息;case5:toastr.error("请选择一个文件");休息;case4:toastr.error("上传失败");休息;案例3:toastr.error(result['info']);休息;情况2:toastr.error("无效文件类型");休息;情况1:toastr.error(result['info']);休息;}}}editor.create();//vareditor=newwangEditor('{$this->id}');//editor.create();EOT;返回父级::渲染();}}新视图文件resources/views/admin/wang-editor.blade.php:has($label)?:'has-error'!!}">{{$label}}@include('admin::form.error'){!!旧($column,$value)!!
然后注册到laravel-admin,在app/Admin/bootstrap.php中添加如下代码:editor('body');3.3完成WangEditor图片上传3.3.1创建上传路由routes/web.phpRoute::post('/uploadFile','UploadsController@uploadImg');3.3.2创建上传文件控制器UploadsControllerphpartisanmake:controllerUploadsController修改app\Controllers\UploadsController.phpfile("mypic");}//dd($文件);if(!empty($file)){foreach($fileas$key=>$value){$len=$key;}if($len>25){returnresponse()->json(['ResultData'=>6,'info'=>'Upto25imagescanbeuploaded']);}$m=0;$k=0;for($i=0;$i<=$len;$i++){//$n表示是哪张图片$n=$i+1;if($file[$i]->isValid()){if(in_array(strtolower($file[$i]->extension()),['jpeg','jpg','gif','gpeg','png'])){$picname=$file[$i]->getClientOriginalName();//获取上传的原始文件名$ext=$file[$i]->getClientOriginalExtension();//获取上面文件扩展名//重命名$filename=time().海峡随机(6)。“。”.$分机;如果($file[$i]->move("上传/图像",$filename)){$newFileName='/'.“上传/图像”。'/'。$文件名;$m=$m+1;//returnresponse()->json(['ResultData'=>0,'info'=>'上传成功','newFileName'=>$newFileName]);}else{$k=$k+1;//returnresponse()->json(['ResultData'=>4,'info'=>'上传失败']);}$味精=$米。“上传图片成功”。$k。"上传图片失败
";$return[]=['ResultData'=>0,'info'=>$msg,'newFileName'=>$newFileName];}else{returnresponse()->json(['ResultData'=>3,'info'=>'No.'.$n.'此图片后缀名无效!
'.'只支持jpeg/jpg/png/gif格式']);}}else{returnresponse()->json(['ResultData'=>1,'info'=>'No.'.$n.'图片超出最大限制!
'.'图片最大支持2M']);}}else{returnresponse()->json(['ResultData'=>5,'info'=>'请选择一个文件']);}返回$return;}}3.3.3修改config/admin.php中的hostupload'upload'=>['disk'=>'admin','directory'=>['image'=>'image','file'=>'file',],//将upload改为uploads'host'=>'http://localhost:8000/uploads/',]],从预览图可以看出WangEditor可以上传多张图片,但是有时候我们文章想加多张封面图,怎么办?下面我们来完成laravel-admin的多图上传4.Laravel-admin多图上传4.1修改app/Admin/PostController中的form()方法,更改$form->image('cover','cover');到$form->multipleImage('cover','Cover');4.2创建图片修改器laravel修改器使用说明,请阅读相关文档在app/Models/Post.php中添加setCoverAttribute()和setCoverAttribute两个方法attributes['cover']=json_encode($cover);}}publicfunctiongetCoverAttribute($cover){returnjson_decode($cover,true);}}显示效果原文地址https://www.bear777.com/blog/laravel-wangeditorgithub地址https://github.com/pandoraxm/laravel-admin-wangeditor