1. xyz/resources/views/mgmt/admin/update_form.blade.php 을 작성한다.

<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <h4 class="modal-title">관리자 수정</h4>
</div>
<div class="modal-body">
  <form name="update_form" action="/mgmt/admin/update" method="post">
    @csrf
    <div class="form-group">
      <label>아이디</label>
      <input type="text" name="userid" class="form-control" readonly required pattern="[a-zA-Z0-9]+" value="{{ $admin->userid }}"/>
      <input type="hidden" name="id" class="form-control" value="{{ $admin->sno }}" />
    </div>
    <div class="form-group">
      <label>별명 <small>(필수)</small></label>
      <input type="text" name="nick" class="form-control" required value="{{ $admin->nick }}"/>
    </div>
    <div class="form-group" style="text-align: right">
      <input class="btn btn-primary" type="submit" value="관리자 수정" />
    </div>
  </form>
</div>

 

2. xyz/app/Http/Controllers/Mgmt/Admin.php 에 다음을 추가한다.

...
 
    // 관리자 수정 폼
    // /mgnt/admin/update_form/{id}
    public function update_form ($id) {
        // $t_admin = DB::select('CALL getAdmin(?)', [$id]);
        // $admin = $t_admin[0];
        $admin = collect(DB::select('CALL getAdmin(?)', [$id]))->first();
        return view('mgmt.admin.update_form', ['admin' => $admin]);
    }
 
...

 

3. xyz/routes/web.php 에 다음을 추가한다.

    Route::get('admin/update_form/{id}', [Admin::class, 'update_form']);

 

4. 이제, 수정 버튼을 클릭하면 수정할 수 있는 폼이 나타나게 될 것이다. 마지막으로 실제로 수정을 처리하는 루틴을 작성하고 라우터에 등록하자. 그전에 updateAdmin 이라는 저장 프로시저부터 만들어야겠지?

 

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 262
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
DELIMITER $$
CREATE PROCEDURE updateAdmin
    (i_sno INT,
     i_nick VARCHAR(255))
BEGIN
    UPDATE admins SET nick = i_nick WHERE sno = i_sno;
END $$
DELIMITER ;
mysql> CALL listAdmins();
+-----+---------+-----------+-------+
| sno | userid  | password  | nick  |
+-----+---------+-----------+-------+
|   1 | testid1 | passwd101 | nick1 |
|   2 | testid2 | passwd2   | nick2 |
|   3 | testid3 | passwd3   | nick3 |
|   4 | testid4 | passwd4   | nick4 |
|   5 | testid5 | passwd5   | nick5 |
|   9 | testid6 | passwd106 | nick6 |
+-----+---------+-----------+-------+
6 ROWS IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> CALL updateAdmin(1, 'nick101');
Query OK, 1 ROW affected (0.00 sec)
 
mysql> CALL listAdmins();
+-----+---------+-----------+---------+
| sno | userid  | password  | nick    |
+-----+---------+-----------+---------+
|   1 | testid1 | passwd101 | nick101 |
|   2 | testid2 | passwd2   | nick2   |
|   3 | testid3 | passwd3   | nick3   |
|   4 | testid4 | passwd4   | nick4   |
|   5 | testid5 | passwd5   | nick5   |
|   9 | testid6 | passwd106 | nick6   |
+-----+---------+-----------+---------+
6 ROWS IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> exit

 

5. xyz/app/Http/Controllers/Mgmt/Admin.php 에 다음을 추가한다.

...
 
    // 관리자 수정
    // /mgmt/admin/update
    public function update (Request $request) {
        $id   = $request->input('id');
        $nick = $request->input('nick');
 
        DB::update('CALL updateAdmin(?, ?)', [$id, $nick]);
        return redirect('/mgmt/admin');
    }
 
...

 

6. xyz/routes/web.php 에 다음을 추가하고, 수정 작업을 진행해보자.

    Route::post('admin/update', [Admin::class, 'update']);

 

1. 저장 프로시저 (getAdmin) 생성

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
DELIMITER $$
CREATE PROCEDURE getAdmin(i_sno INT)
BEGIN
  SELECT sno, userid, password, nick FROM admins WHERE sno = i_sno LIMIT 1;
END $$
DELIMITER ;
mysql> CALL getAdmin(2);
+------+---------+----------+-------+
| sno  | userid  | password | nick  |
+------+---------+----------+-------+
|    2 | testid2 | passwd2  | nick2 |
+------+---------+----------+-------+
1 ROW IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> exit

 

2. 뷰 디렉토리에 xyz/resources/views/mgmt/admin/chg_passwd_form.blade.html 파일을 생성한다.

<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <h4 class="modal-title">관리자 비밀번호 변경</h4>
</div>
<div class="modal-body">
  <form name="chg_passwd_form" action="/mgmt/admin/chg_passwd" method="post">
    @csrf
    <div class="form-group">
      <label>아이디</label>
      <input type="text" name="userid" class="form-control" readonly required value="{{ $admin->userid }}"/>
      <input type="hidden" name="id" value="{{ $admin->sno }}" />
    </div>
    <div class="form-group">
      <label>비밀번호 <small>(필수)</small></label>
      <input type="password" name="passwd1" class="form-control" required />
    </div>
    <div class="form-group">
      <label>비밀번호 확인 <small>(필수)</small></label>
      <input type="password" name="passwd2" class="form-control" required />
    </div>
    <div class="form-group" style="text-align: right">
      <input class="btn btn-primary" type="submit" value="관리자 비밀번호 변경" />
    </div>
  </form>
</div>

 

3. 컨트롤러(xyz/app/Http/Controllers/Mgmt/Admin.php)에 다음을 추가한다.

...
 
    // 관리자 비밀번호변경 폼
    // /mgnt/admin/chg_passwd_form/{id}
    public function chg_passwd_form ($id) {
        $t_admin = DB::select('CALL getAdmin(?)', [$id]);
        $admin = $t_admin[0];
        // $admin = collect(DB::select('CALL getAdmin(?)', [$id]))->first();
        return view('mgmt.admin.chg_passwd_form', ['admin' => $admin]);
    }
 
...

 

4. 라우터(xyz/routes/web.php)에 다음을 추가한다.

Route::get('admin/chg_passwd_form/{id}', [Admin::class, 'chg_passwd_form']);

 

5. 이제, 비밀번호변경 버튼을 클릭하면 비밀번호변경을 위한 모달 다이얼로그박스가 나타날 것이다.

 

6. 실제로 비밀번호를 변경하는 작업을 해보자. 우선 저장 프로시저 (updateAdminPassword) 생성하자.

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql> DELIMITER $$
mysql> CREATE PROCEDURE updateAdminPassword
    -> (i_sno INT,
    ->  i_password VARCHAR(255))
    -> BEGIN
    ->   UPDATE admins SET password = i_password WHERE sno = i_sno;
    -> END $$
Query OK, 0 ROWS affected (0.01 sec)
 
mysql> DELIMITER ;
mysql>
mysql> CALL listAdmins();
+-----+---------+----------+-------+
| sno | userid  | password | nick  |
+-----+---------+----------+-------+
|   1 | testid1 | passwd1  | nick1 |
|   2 | testid2 | passwd2  | nick2 |
|   3 | testid3 | passwd3  | nick3 |
|   4 | testid4 | passwd4  | nick4 |
|   5 | testid5 | passwd5  | nick5 |
|   9 | testid6 | passwd6  | nick6 |
+-----+---------+----------+-------+
6 ROWS IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> CALL updateAdminPassword(1, 'passwd101');
Query OK, 1 ROW affected (0.00 sec)
 
mysql> CALL listAdmins();
+-----+---------+-----------+-------+
| sno | userid  | password  | nick  |
+-----+---------+-----------+-------+
|   1 | testid1 | passwd101 | nick1 |
|   2 | testid2 | passwd2   | nick2 |
|   3 | testid3 | passwd3   | nick3 |
|   4 | testid4 | passwd4   | nick4 |
|   5 | testid5 | passwd5   | nick5 |
|   9 | testid6 | passwd6   | nick6 |
+-----+---------+-----------+-------+
6 ROWS IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> exit

 

7. 그리고, 컨트롤러(xyz/app/Http/Controllers/Mgmt/Admin.php)에 다음을 추가한다.

    // 관리자 비밀번호변경
    // /mgmt/admin/chg_passwd
    public function chg_passwd (Request $request) {
        $id      = $request->input('id');
        $passwd1 = $request->input('passwd1');
        $passwd2 = $request->input('passwd2');
 
        if ($passwd1 != $passwd2) {
            return redirect('/mgmt/admin');
        }
        DB::update('CALL updateAdminPassword(?, ?)', [$id, $passwd1]);
 
        return redirect('/mgmt/admin');
    }

 

8. 라우터(xyz/routes/web.php)에는 다음을 추가한다.

    Route::post('admin/chg_passwd', [Admin::class, 'chg_passwd']);

 

9. 비밀번호를 변경해보고, DB의 내용이 잘 반영되었는지 확인해보자.

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 241
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)
 
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> CALL listAdmins();
+-----+---------+-----------+-------+
| sno | userid  | password  | nick  |
+-----+---------+-----------+-------+
|   1 | testid1 | passwd101 | nick1 |
|   2 | testid2 | passwd2   | nick2 |
|   3 | testid3 | passwd3   | nick3 |
|   4 | testid4 | passwd4   | nick4 |
|   5 | testid5 | passwd5   | nick5 |
|   9 | testid6 | passwd106 | nick6 |
+-----+---------+-----------+-------+
6 rows in set (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
mysql>

 

1. 저장 프로시저 (insertAdmin) 생성

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
DELIMITER $$
CREATE PROCEDURE insertAdmin
  (userid VARCHAR(255),
   password VARCHAR(255),
   nick VARCHAR(255))
BEGIN
  INSERT INTO admins(userid, password, nick) VALUES(userid, password, nick);
END $$
DELIMITER ;
mysql> SELECT * FROM admins;
+-----+---------+----------+-------+
| sno | userid  | password | nick  |
+-----+---------+----------+-------+
|   1 | testid1 | passwd1  | nick1 |
|   2 | testid2 | passwd2  | nick2 |
|   3 | testid3 | passwd3  | nick3 |
|   4 | testid4 | passwd4  | nick4 |
|   5 | testid5 | passwd5  | nick5 |
+-----+---------+----------+-------+
5 ROWS IN SET (0.00 sec)
 
mysql> CALL insertAdmin('testid6', 'passwd6', 'nick6');
Query OK, 1 ROW affected (0.01 sec)
 
mysql> SELECT * FROM admins;
+-----+---------+----------+-------+
| sno | userid  | password | nick  |
+-----+---------+----------+-------+
|   1 | testid1 | passwd1  | nick1 |
|   2 | testid2 | passwd2  | nick2 |
|   3 | testid3 | passwd3  | nick3 |
|   4 | testid4 | passwd4  | nick4 |
|   5 | testid5 | passwd5  | nick5 |
|   6 | testid6 | passwd6  | nick6 |
+-----+---------+----------+-------+
6 ROWS IN SET (0.00 sec)
 
mysql> exit

 

2. xyz/resources/views/mgmt/admin/index.blade.php 에 다음을 추가한다.

...
 
<div style="text-align: right; padding-bottom: 10px">
  <a href="/mgmt/admin/insert_form" class="btn btn-default" data-toggle="modal" data-target="#myModal">관리자 추가</a>
</div>
 
...
 
<div id="myModal" class="modal fade" role="dialog" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
    </div>
  </div>
</div>
 
...
 
<script>
// Modal Remote Reload
$(document).on('hidden.bs.modal', function (e) {
    $(e.target).removeData('bs.modal');
})
</script>
 
...

 

3. http://xyz.test.com/mgmt/admin 을 다시 보면, 관리자 추가 버튼이 새로이 나타날 것이다.

 

4. 관리자 입력 양식을 만들자. xyz/resources/views/mgmt/admin/insert_form.blade.php

<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <h4 class="modal-title">관리자 추가</h4>
</div>
<div class="modal-body">
  <form name="insert_form" action="/mgmt/admin/insert" method="post">
    @csrf
    <div class="form-group">
      <label>아이디 <small>(필수)</small></label>
      <input type="text" name="userid" class="form-control" required>
    </div>
    <div class="form-group">
      <label>비밀번호 <small>(필수)</small></label>
      <input type="password" id="password" name="passwd1" class="form-control" required>
    </div>
    <div class="form-group">
      <label>비밀번호 확인 <small>(필수)</small></label>
      <input type="password" name="passwd2" class="form-control" required>
    </div>
    <div class="form-group">
      <label>별명 <small>(필수)</small></label>
      <input type="text" name="nick" class="form-control" required>
    </div>
    <div class="form-group" style="text-align: right">
      <input class="btn btn-primary" type="submit" value="관리자 추가" />
    </div>
  </form>
</div>

 

5. xyz/app/Http/Controllers/Mgmt/Admin.php 에 다음 코드를 추가한다.

...
 
    // 관리자 추가 폼
    public function insert_form () {
        return view('mgmt.admin.insert_form');
    }
 
...

 

6. xyz/routes/web.php 에서 mgmt 그룹안에 다음을 추가한다.

    Route::get('admin/insert_form', [Admin::class, 'insert_form']);

 

7. 관리자 추가 버튼을 클릭하면 모달 다이얼로그 박스 형식의 입력 양식이 나올 것이다.

 

8. 이제, 실제로 DB에 관리자를 추가해보자. 이제부터는 Model 클래스를 작성하지 않는다. Controller 에서 직접 DB에 접근해서 Stored Procedure 를 호출할 것이다. xyz/app/Http/Controllers/Mgmt/Admin.php 에 다음을 추가한다.

...
 
use Illuminate\Http\Request;
 
...
 
    // 관리자 추가
    public function insert (Request $request) {
        $userid  = $request->input('userid');
        $passwd1 = $request->input('passwd1');
        $passwd2 = $request->input('passwd2');
        $nick    = $request->input('nick');
 
        if ($passwd1 != $passwd2) {
            return redirect('/mgmt/admin');
        }
        // $model = new AdminModel();
        // $model->insertAdmin($userid, $passwd1, $nick);
        DB::insert('CALL insertAdmin(?, ?, ?)', [$userid, $passwd1, $nick]);
 
        return redirect('/mgmt/admin');
    }
 
...

 

9. xyz/routes/web.php 에서 mgmt 그룹안에 다음을 추가한다.

    Route::post('admin/insert', [Admin::class, 'insert']);

 

10. 코드 작성은 완료되었으니, 실제 화면에서 관리자를 등록해보면 목록에 표시되는 것을 볼 수 있을 것이다.

 

 

 

1. 관련 모듈 설치

$ sudo apt-get install php-mysql

 

2. DB연결 설정

$ sudo vi ~/project/xyz/.env
...

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=xyz
DB_USERNAME=xyz
DB_PASSWORD=xyz123

...

 

3. 저장 프로시저 (listAdmins) 생성

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
DELIMITER $$
CREATE PROCEDURE listAdmins()
BEGIN
  SELECT sno, userid, password, nick FROM admins;
END $$
DELIMITER ;
mysql> CALL listAdmins();
+-----+---------+----------+-------+
| sno | userid  | password | nick  |
+-----+---------+----------+-------+
|   1 | testid1 | passwd1  | nick1 |
|   2 | testid2 | passwd2  | nick2 |
|   3 | testid3 | passwd3  | nick3 |
|   4 | testid4 | passwd4  | nick4 |
|   5 | testid5 | passwd5  | nick5 |
+-----+---------+----------+-------+
5 ROWS IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)

 

4. xyz/app/Models/AdminModel.php 파일을 생성한다.

<?php
 
namespace App\Models;
 
use Illuminate\Support\Facades\DB;
 
class AdminModel
{
    public function listAdmins()
    {
        // $admins = DB::select('select * from admins');
        $admins = DB::select('CALL listAdmins()');
        return $admins;
    }
 
}

 

5. xyz/app/Http/Controllers/Mgmt/Admin.php 파일을 생성한다.

<?php
 
namespace App\Http\Controllers\Mgmt;
 
use App\Http\Controllers\Controller;
use App\Models\AdminModel;
// use Illuminate\Support\Facades\DB;

class Admin extends Controller
{
    public function index()
    {
        // $admins = DB::select('CALL listAdmins()');
        // 저장프로시저를 사용하므로 DB작업이 간단하다.
        // 재사용 계획이 없는 경우에는 Model 작성하지 않고, 
        // Controller 에서 직접 DB작업을 하는 것이 좋다.
        $adminModel = new AdminModel;
        $admins = $adminModel->listAdmins();
        return view('mgmt.admin.index', ['admins' => $admins]);
    }
}

저장프로시저를 사용하므로 DB작업이 간단하다. 재사용 계획이 없는 경우에는 Model 작성하지 않고, Controller 에서 직접 DB작업을 하는 것이 좋다. 다음부터는 Model을 만들지 않겠다.

 

6. xyz/resources/views/mgmt/base.blade.php 파일을 생성한다.

<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>Adonis Tutorial</title>
</head>
<body>
  @yield('content')
</body>
</html>

 

7. xyz/resources/views/mgmt/admin/index.blade.php 파일을 생성한다.

@extends('mgmt.base')
 
@section('content')
<div class="container">
  <div class="page-header">
    <h1>Administrator (관리자)</h1>
  </div>
 
  <table class="table table-striped table-hover table-condensed">
  <tr>
    <th style="text-align: center">아이디</th>
    <th style="text-align: center">별명</th>
    <th style="text-align: center">수정/삭제</th>
  </tr>
  @foreach ($admins as $admin)
  <tr>
    <td style="text-align: center">{{$admin->userid}}</td>
    <td style="text-align: center">{{$admin->nick}}</td>
    <td style="text-align: center">
      <a href="/mgmt/admin/chg_passwd_form/{{$admin->sno}}" class="btn btn-default btn-xs" data-toggle="modal" data-target="#myModal">비밀번호변경</a>
      <a href="/mgmt/admin/update_form/{{$admin->sno}}" class="btn btn-default btn-xs" data-toggle="modal" data-target="#myModal">수정</a>
      <button onclick="delete_admin('/mgmt/admin/delete/{{$admin->sno}}')" class="btn btn-default btn-xs">삭제</button>
    </td>
  </tr>
  @endforeach
  </table>
</div>
 
<script>
function delete_admin(url) {
    var result = confirm("관리자를 정말로 삭제하시겠습니까?");
    if( result == false ) return;
    location.href = url;
}
</script>
@endsection

 

8. xyz/routes/web.php 에 다음을 추가한다.

...

use App\Http\Controllers\Mgmt\Admin;

...

Route::prefix('mgmt')->group(function () {
    Route::get('admin', [Admin::class, 'index']);
});

...

 

9. 이제, 웹브라우저에서 http://xyz.test.com/mgmt/admin 에 접속하면 관리자 목록을 볼 수 있을 것이다.

 

+ Recent posts