PHP강좌/[라라벨]게시판만들기강좌
[라라벨+mysql]게시판 만들기 강좌 #21. 웹에디터(섬머노트) 적용 feat. iframe 1/3
에스크리토
2024. 8. 1. 12:58
반응형
웹에티더를 적용해보자. 섬머노트를 사용할건데 이전 강좌에서 많이 다뤘으니까 익숙할 것이다. 글쓰기 페이지에 iframe을 사용해서 적용해 보겠다.
/resources/views/boards/write.blade.php
@extends('boards.layout')
@section('header')
<?php
if($bid){
$btitle = "수정";
}else{
$btitle = "쓰기";
}
?>
@include('boards.toptitle', ['toptitle'=>'게시판 '.$btitle, 'multi'=>$multi])
@endsection
@section('content')
<br />
<form method="post" action="/boards/create" enctype="multipart/form-data">
@csrf
@method('post')
<input type="hidden" name="pid" id="pid" value="{{ $bid??time() }}">
<input type="hidden" name="bid" id="bid" value="{{ $bid??0 }}">
<input type="hidden" name="code" id="code" value="boardattach">
<input type="hidden" name="attcnt" id="attcnt" value="0">
<input type="hidden" name="imgUrl" id="imgUrl" value="">
<div class="form-group">
<div class="col-md-12">
<input type="text" name="subject" id="subject" class="form-control input-lg" placeholder="제목을 입력하세요." value="{{ $boards->subject??'' }}" />
</div>
<br />
</div>
<div class="form-group">
<div class="col-md-12">
<iframe id="summerframe" src="{{ route('boards.summernote', $multi) }}" style="width:100%; height:450px; border:none" scrolling = "no"></iframe>
</div>
</div>
<br />
<div class="form-group">
@if($attaches)
<div id="attach_site" class="col-md-12">
<div class="row row-cols-1 row-cols-md-6 g-4" id="attachFiles" style="margin-left:0px;">
@foreach ($attaches as $att)
@if($att)
<div id="af_{{ $att->id }}" class='card h-100' style='width:120px;margin-right: 10px;margin-bottom: 10px;'><img src="/images/{{ $att->filename }}" width='100' /><div class='card-body'><button type='button' class='btn btn-warning' onclick="deletefile('{{ $att->filename }}','{{ $att->id }}')">삭제</button></div></div>
@endif
@endforeach
</div>
</div>
@else
<div id="attach_site" class="col-md-12">
<div class="row row-cols-1 row-cols-md-6 g-4" id="attachFiles" style="margin-left:0px;">
</div>
</div>
@endif
<div class="col-md-12">
<input type="file" name="afile" id="afile" multiple accept="image/*" multiple class="form-control" aria-label="Large file input example">
</div>
</div>
<br />
<br />
<div class="form-group">
@if($bid)
<div class="col-md-12 text-center">
<button type="button" name="edit" class="btn btn-primary input-lg" onclick="updatesubmit()">수정</button>
</div>
@else
<div class="col-md-12 text-center">
<button type="button" name="edit" class="btn btn-primary input-lg" onclick="sendsubmit()">등록</button>
</div>
@endif
</div>
</form>
<script>
$("#afile").change(function(){
var formData = new FormData();
var attcnt=$("#attcnt").val();
var files = $('#afile').prop('files');
var totcnt=parseInt(attcnt)+parseInt(files.length)
if(totcnt>5){
alert('5개까지만 등록할 수 있습니다.');
return false;
}
for(var i=0; i < files.length; i++) {
attachFile(files[i]);
}
});
function attachFile(file) {
var formData = new FormData();
var pid = $("#pid").val();
var code = $("#code").val();
formData.append("file", file);
formData.append("uptype", "attach");
formData.append("pid", pid);
formData.append("code", code);
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
url: '{{ route('boards.saveimage') }}',
data: formData,
cache: false,
contentType: false,
processData: false,
dataType : 'json' ,
type: 'POST',
success: function (return_data) {
// console.log(JSON.stringify(return_data));
if(return_data.result=='fail'){
alert(return_data.msg);
return false;
}else{
//var img="<img src='"+data+"' width='50'><br>";
var html = "<div id='af_"+return_data.fid+"' class='card h-100' style='width:120px;margin-right: 10px;margin-bottom: 10px;'><img src='/images/"+return_data.fn+"' width='100' /><div class='card-body'><button type='button' class='btn btn-warning' onclick=\"deletefile('"+return_data.fn+"', '"+return_data.fid+"')\">삭제</button></div></div>";
$("#attachFiles").append(html);
var rcnt=parseInt(attcnt)+1;
$("#attcnt").val(rcnt);
var attachFile=$("#attachFile").val();
if(attachFile){
attachFile=attachFile+",";
}
$("#attachFile").val(attachFile+return_data.fn);
}
}
, beforeSend: function () {
var width = 0;
var height = 0;
var left = 0;
var top = 0;
width = 50;
height = 50;
top = ( $(window).height() - height ) / 2 + $(window).scrollTop();
left = ( $(window).width() - width ) / 2 + $(window).scrollLeft();
if($("#div_ajax_load_image").length != 0) {
$("#div_ajax_load_image").css({
"top": top+"px",
"left": left+"px"
});
$("#div_ajax_load_image").show();
}
else {
$('body').append('<div id="div_ajax_load_image" style="position:absolute; top:' + top + 'px; left:' + left + 'px; width:' + width + 'px; height:' + height + 'px; z-index:9999;" class="spinner-border" role="status"><span class="visually-hidden">Loading...</span></div>');
}
}
, complete: function () {
$("#div_ajax_load_image").hide();
}
});
}
function deletefile(fn,fid){
var pid = $("#pid").val();
var code = $("#code").val();
var data = {
fn : fn,
pid : pid,
code : code
};
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
type: 'post',
url: '{{ route('boards.deletefile') }}',
dataType: 'json',
data: data,
success: function(data) {
alert("삭제했습니다.");
$("#af_"+fid).hide();
},
error: function(data) {
console.log("error" +JSON.stringify(data));
}
});
}
function sendsubmit(){
var subject=$("#subject").val();
var content=$("#content").val();
var pid = $("#pid").val();
var code = $("#code").val();
var data = {
multi : '{{ $multi }}',
subject : subject,
content : content,
pid : pid,
code : code
};
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
type: 'post',
url: '{{ route('boards.create') }}',
dataType: 'json',
enctype: 'multipart/form-data',
data: data,
success: function(data) {
location.href='/boards/show/'+data.bid+'/1';
},
error: function(data) {
console.log("error" +data);
}
});
}
function updatesubmit(){
var subject=$("#subject").val();
var content=$("#content").val();
var bid='{{ $bid }}';
var data = {
subject : subject,
content : content,
bid : bid
};
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
type: 'post',
url: '{{ route('boards.update') }}',
dataType: 'json',
enctype: 'multipart/form-data',
data: data,
success: function(data) {
location.href='/boards/show/'+data.bid+'/1';
},
error: function(data) {
console.log("error" +data);
}
});
}
</script>
@endsection
글쓰는 textarea를 빼고 iframe을 넣었다. iframe의 src에 어떤 파일을 불러 올지 적어놨으니 그 파일을 불러 올 수 있게 라우터와 콘트롤러를 수정한다.
/routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BoardController;
use App\Http\Controllers\MemberController;
Route::get('/', function () {
return view('welcome');
});
//게시판
Route::get('/boards/{multi?}', [BoardController::class, 'index'])->name('boards.index');
Route::get('/boards/show/{id}/{page}', [BoardController::class, 'show'])->name('boards.show');
Route::middleware('auth') -> group(function (){
Route::get('/boards/write/{multi}/{bid?}', [BoardController::class, 'write'])->name('boards.write');
Route::post('/boards/create', [BoardController::class, 'create'])->name('boards.create');
Route::post('/boards/saveimage', [BoardController::class, 'saveimage'])->name('boards.saveimage');
Route::post('/boards/deletefile', [BoardController::class, 'deletefile'])->name('boards.deletefile');
Route::get('/boards/imgpop/{imgfile}', [BoardController::class, 'imgpop'])->name('boards.imgpop');
Route::post('/boards/update', [BoardController::class, 'update'])->name('boards.update');
Route::get('/boards/delete/{bid}/{page}', [BoardController::class, 'delete'])->name('boards.delete');
Route::get('/boards/summernote/{multi}', [BoardController::class, 'summernote'])->name('boards.summernote');
});
//회원
Route::get('/login', [MemberController::class, 'login'])->name('auth.login');
Route::get('/signup', [MemberController::class, 'signup'])->name('auth.signup');
Route::post('/signupok', [MemberController::class, 'signupok'])->name('auth.signupok');
Route::post('/emailcheck', [MemberController::class, 'emailcheck'])->name('auth.emailcheck');
Route::post('/loginok', [MemberController::class, 'loginok']) -> name('auth.loginok');
Route::post('/logout', [MemberController::class, 'logout']) -> name('auth.logout');
/app/Http/Controllers/BoardController.php
public function summernote($multi)
{
return view('boards.summernote', ['multi' => $multi]);
}
이렇게 해주고 콘트롤러에서 불러온다고 한 파일을 만들어 주면 된다.
/resources/views/boards/summernote.blade.php
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote.min.js"></script>
<div id="summernote"></div>
<script>
$(document).ready(function() {
var $summernote = $('#summernote').summernote({
codeviewFilter: false,
codeviewIframeFilter: true,
lang: 'ko-KR',
height: 400
});
});
</script>
이렇게 뷰 파일까지 만들고 저장하면 끝이다. 이제 글쓰기 페이지에 들어가보면
이렇게 보이면 성공이다. 안되면 될때까지 해봐야 한다. 안그러면 진도를 나갈 수가 없다.
다음엔 글을 작성하고 저장하는 걸 해보도록 하자.
반응형