라라벨, laravel 로 샘플 만들어 보자. (4)(end) view , controller

2

controller 수정

app\Http\Controllers\SimpleCommentController.php

<?php

namespace App\Http\Controllers;

use App\SimpleComment;
use Illuminate\Http\Request;

class SimpleCommentController extends Controller
{
  /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
  public function index()
  {
    return [
      'result'=>true,
      'count' => SimpleComment::count(),
      'simpleComments' => SimpleComment::lists(),
    ];
    // return View('sc/index');
    //
  }
  
  /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
  public function store(Request $request)
  {
    //
  }
  
  /**
  * Display the specified resource.
  *
  * @param  \App\SimpleComment  $simpleComment
  * @return \Illuminate\Http\Response
  */
  public function show(SimpleComment $simpleComment)
  {
    //
  }
  
  /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \App\SimpleComment  $simpleComment
  * @return \Illuminate\Http\Response
  */
  public function update(Request $request, SimpleComment $simpleComment)
  {
    if(!$simpleComment->checkScPass($request->input('sc_pass'))){
      return [
        'result'=>false,
        'message'=>'Incorrect password',
      ];
    }
    $simpleComment->update($request->all());
    return $this->index();
}
  
  /**
  * Remove the specified resource from storage.
  *
  * @param  \App\SimpleComment  $simpleComment
  * @return \Illuminate\Http\Response
  */
  // $request 를 추가, 제어 역전으로 알아서 데이터를 전달 해 준다.
  public function destroy(Request $request, SimpleComment $simpleComment)
  {
    if(!$simpleComment->checkScPass($request->input('sc_pass'))){
      return [
        'result'=>false,
        'message'=>'Incorrect password',
      ];
    }
    $simpleComment->delete();
    return $this->index();
  }
}

API쪽 route 수정 (경로가 모델명에 안 맞으면 컨트롤러에서 모델을 제대로 못 가져옮)

routes\api.php

Route::resource('sc', 'SimpleCommentController');
Route::resource('simpleComment', 'SimpleCommentController');

web 쪽 route 추가

routes\web.php
Route::any('/sc','WebSimpleCommentController@index');

web 쪽 controller 추가

app\Http\Controllers\WebSimpleCommentController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class WebSimpleCommentController extends Controller
{
protected $title='SimpleComment';
    public function index($value='')
    {
    return View('sc.lists',['title'=>$this->title]);
    }
}

web 쪽 view 파일

resources\views\sc\lists.blade.php
@extends('sc.master')

@section('title', $title)

@section('content')
<h1>SimpleComment</h1>
<div id="sc_app">
<ul class="list-group"  v-for="sc in res.simpleComments">
<li class="list-group-item">
<form onsubmit="ajax_action.action(this);return false;" action="{{ route('simpleComment.update',':sc_id') }}">
{{ csrf_field() }}
<input type="text" name="_method" disabled value="" />
<input type="text" name="sc_id" readonly v-model="sc.sc_id" />
<input type="password" name="sc_pass"/>
<input type="text" name="sc_name" v-model="sc.sc_name" />
<input type="text" name="sc_comment" v-model="sc.sc_comment" />
<input disabled type="created_at" v-model="sc.created_at" />
<input disabled type="updated_at" v-model="sc.updated_at" />
<button onclick="this.form._method.value='PUT'">modify</button>
<button onclick="this.form._method.value='DELETE'">delete</button>

</form>
</li>
</ul>
</div>
<script>
window.Laravel = {!! json_encode([
 'csrfToken' => csrf_token(),
]) !!};
        // vue.js의 app을 생성
var sc_app = new Vue({
el: '#sc_app',
data: {
res:{'result':null,message:'','count':0,'simpleComments':[]},
}
})
$(function(){
ajax_action.lists();
})
var ajax_action = {
"lists":function(){
var url = "{{ route('simpleComment.index') }}";
$.ajax({
url: url,
type: 'GET', //GET
dataType: 'json', //xml, json, script, jsonp, or html
// data: post_data,
})
.done(function(rData) { //통신 성공 시 호출
console.log("success");
if(!rData.result){
alert(rData.message)
}else{
sc_app.res = rData;
}
})
.fail(function() { //통신 실패 시 호출
console.log("error");
})
.always(function() { //성공/실패 후 호출.
console.log("complete");
});
},
"action":function(f){
var url = f.action.replace(":sc_id",f.sc_id.value);
var post_data = $(f).serialize();
$.ajax({
url: url,
type: f._method.value, //GET
dataType: 'json', //xml, json, script, jsonp, or html
data: post_data,
})
.done(function(rData) { //통신 성공 시 호출
console.log("success");
if(!rData.result){
alert(rData.message)
}else{
sc_app.res = rData;
}
})
.fail(function(e) { //통신 실패 시 호출
console.log(e);
})
.always(function() { //성공/실패 후 호출.
console.log("complete");
});
f._method.value = '';
}
}
</script>

<code style="white-space:pre">
API LISTS
+--------+-----------+------------------+------------+------------------------------------------------------+------------+
| Domain | Method    | URI              | Name       | Action                                               | Middleware |
+--------+-----------+------------------+------------+------------------------------------------------------+------------+
|        | GET|HEAD  | api/sc           | sc.index   | App\Http\Controllers\SimpleCommentController@index   | api        |
|        | POST      | api/sc           | sc.store   | App\Http\Controllers\SimpleCommentController@store   | api        |
|        | GET|HEAD  | api/sc/create    | sc.create  | App\Http\Controllers\SimpleCommentController@create  | api        |
|        | GET|HEAD  | api/sc/{sc}      | sc.show    | App\Http\Controllers\SimpleCommentController@show    | api        |
|        | PUT|PATCH | api/sc/{sc}      | sc.update  | App\Http\Controllers\SimpleCommentController@update  | api        |
|        | DELETE    | api/sc/{sc}      | sc.destroy | App\Http\Controllers\SimpleCommentController@destroy | api        |
|        | GET|HEAD  | api/sc/{sc}/edit | sc.edit    | App\Http\Controllers\SimpleCommentController@edit    | api        |
+--------+-----------+------------------+------------+------------------------------------------------------+------------+
</code>
@endsection

댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -
목록형 📷 갤러리형
제목
[기본형] HTML (with 부트스트랩5.3 , jquery 3.7, vue.js)
유용한 리눅스(LINUX) 명령어
[공지] 기술 게시판
10.27
10.29
10.30
10.31
11.1
11.2
11.3
11.4
11.5
11.6
11.7
11.9
11.10
11.11
11.12
11.13
11.14
11.15
11.16
11.17
11.18
11.19
11.20
11.21
11.22
11.23
11.24
11.25
11.26
11.27
11.28
11.29
11.30