반응형

게시판 내용을 보기 위해서는 대부분 게시물의 리스트에서 제목을 클릭해서 보는 경우가 될것이다. 우선 게시판 리스트에 링크를 걸어보자.

 

http://localhost:8080/board

 

/app/Views/board_list.php

 

    <table class="table">
        <thead>
            <tr>
            <th scope="col">번호</th>
            <th scope="col">글쓴이</th>
            <th scope="col">제목</th>
            <th scope="col">등록일</th>
            </tr>
        </thead>
        <tbody id="board_list">
            <?php
            foreach($list as $ls){
            ?>
                <tr>
                    <th scope="row"><?php echo $ls->bid;?></th>
                    <td><?php echo $ls->userid;?></td>
                    <td><a href="/boardView/<?php echo $ls->bid;?>"><?php echo $ls->subject;?></a></td>
                    <td><?php echo $ls->regdate;?></td>
                </tr>
            <?php }?>
        </tbody>
        </table>

링크를 보자. <a href="/boardView/<?php echo $ls->bid;?>"> 이렇게 해줬다. 잘 봐두자. 전에 ci4가 아닌 일반 php에서의 링크는 조금 달랐다. <a href="/board/index.php?bid=<?php echo $ls->bid;?>"> 아마 이런 식이었을 것이다. ci4에서는 라우트명에 게시판 번호를 붙여줬다. 

 

그럼 라우트에서는 이걸 어떻게 받을까? 라우트를 수정해보자.

 

/app/Config/Routes.php

 

<?php

namespace Config;

// Create a new instance of our RouteCollection class.
$routes = Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (is_file(SYSTEMPATH . 'Config/Routes.php')) {
    require SYSTEMPATH . 'Config/Routes.php';
}

/*
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
// The Auto Routing (Legacy) is very dangerous. It is easy to create vulnerable apps
// where controller filters or CSRF protection are bypassed.
// If you don't want to define all routes, please use the Auto Routing (Improved).
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
// $routes->setAutoRoute(false);

/*
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');
$routes->get('/board', 'Board::list');
$routes->get('/boardWrite', 'Board::write');
$routes->get('/boardView/(:num)', 'Board::view/$1');

/*
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need it to be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (is_file(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
    require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}

 

$routes->get('/boardView/(:num)', 'Board::view/$1');

 

이부분이다. url에서 어디에 입력한 값인지 지정해주고 컨트롤러에 그 값을 넘겨준다. 그럼 컨트롤러에서 받는 법도 알아보자.

 

/app/Controllers/Board.php

 

<?php

namespace App\Controllers;
use App\Models\BoardModel;//사용할 모델을 반드시 써줘야한다.

class Board extends BaseController
{
    public function list()
    {
        $db = db_connect();
        $query = "select * from board order by bid desc";
        $rs = $db->query($query);
        $data['list'] = $rs->getResult();//결과값 저장
        return render('board_list', $data);//view에 리턴
    }

    public function write()
    {
        return render('board_write');  
    }

    public function view($bid = null)
    {
        $db = db_connect();
        $query = "select * from board where bid=".$bid;
        $rs = $db->query($query);
        $data['view'] = $rs->getRow();

        return render('board_view', $data);  
    }
}

 

view함수에 파라미터를 추가해줬다. $bid = null 이란 부분이다. 만약 라우트에서 보내주는 값이 없으면 null이다. bid를 디비에 조회해서 값을 리턴해준다. 뷰를 확인해보자.

 

/app/Views/board_view.php

 

<h3 class="pb-4 mb-4 fst-italic border-bottom" style="text-align:center;">
        - 게시판 보기 -
      </h3>

      <article class="blog-post">
        <h2 class="blog-post-title"><?php echo $view->subject;?></h2>
        <p class="blog-post-meta"><?php echo $view->regdate;?> by <a href="#"><?php echo $view->userid;?></a></p>

        <hr>
       
        <p>
        <?php echo $view->content;?>
        </p>
       
       
        <hr>
      </article>

뷰는 간단하다. 넘겨받은 값을 뿌려주기만 하면 된다.

 

참 다시봐도 지랄맞지 않은가? 뭐가 이리 복잡한지....

 

다음엔 글쓰기를 해보도록 하자.

반응형

+ Recent posts