Framework (Backend)/Laravel

(Lavavel) 관리자 목록 보여주기 : Laravel 8.x

가우리언 2021. 8. 23.
728x90
반응형

 

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 에 접속하면 관리자 목록을 볼 수 있을 것이다.

 

728x90
반응형

댓글