브라우저에서 https://windows.php.net/download/ 에 접속합니다. 오른쪽에서 원하는 버전의 ZIP 파일을 다운로드 받습니다. 그리고, 화면 왼쪽 VC15 & VS16 항목에서 "Visual C++ Redistributable for Visual Studio 2015-2019" VC_redist.x64.exe 도 다운로드받아서 설치합니다. 이미 설치되어 있으면 설치하지 않아도 됩니다. 그리고, PHP ZIP 파일을 적당한 곳에 풀어서 폴더명도 정해봅니다. 저는 D:\bin\php-8.1.6 에 풀었습니다.

 

환경설정을 위해서 설정 > 정보 > 고급 시스템 설정 을 선택합니다.

 

시스템 속성 창에서 "환경 변수..." 버튼을 클릭한다.

 

환경 변수 창에서 Path를 찾아서 "편집..." 버튼을 클릭합니다.

 

"새로 만들기" 버튼을 클릭하고, PHP가 설치된 폴더를 지정하고, "확인" 버튼을 클릭해서 마무리한다.

 

커맨드 창을 열어서 php -v 를 실행해서 다음처럼 나오면 성공이다.

 

php.ini 파일에서 필요한 extension 이 있다면, 주석을 풀어주자.

 

마지막으로, https://getcomposer.org/Composer-Setup.exe 를 다운로드 받아서, Composer 까지 설치해 놓자.

 

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. 관련 모듈 설치

$ 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