1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】Laravel課題:10-1 ひとこと掲示板

Last updated at Posted at 2021-06-13

1・利用者が名前とコメントを入力し、発言できる。
2・利用者の過去の発言内容をCSVファイルで管理する。
3・全ての利用者の過去の発言内容を一覧で表示する。
4・最新の書き込みが一番上に表示されるようにする。
5・一覧には「名前」「コメント」「発言日時」の3つを1行ずつ表示する。

ルーティング

/routes/web.php

<?php
Route::get('/simple_bbs', 'SimpleBBSController@index');

Route::post('/simple_bbs', 'SimpleBBSController@store');

コントローラー

app/Http/Controllers/SimpleBBSController.php.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SimpleBBSController extends Controller
{
    public function index(){
        
      $file = new \SplFileObject(storage_path('app/bbs.csv'));
 
      // 読み込み設定
      $file->setFlags(
        \SplFileObject::READ_CSV | // CSVを配列形式で読み込む
        \SplFileObject::READ_AHEAD |
        \SplFileObject::SKIP_EMPTY | // 前の行と合わせて、空行があったら読み飛ばす
        \SplFileObject::DROP_NEW_LINE // 改行コードは無視する
      );
      // 1行ずつ読み込んで配列に保存
      $boards = [];
      foreach($file as $board){
        $boards[] = $board;
      }
      
     return view('bbs.index', [
        'boards' => $boards,
        'title' => 'メールアドレス帳',
      ]);
    
    }
    
    public function store(Request $request){
        
      $file = new \SplFileObject(storage_path('app/bbs.csv'), 'a');

      $request->validate([

        'username' => ['required', 'min:2' ,'max:20'],
        'comment'   => ['required', 'min:2' ,'max:100'],
        
      ]);

        $date  = date('Y-m-d H:i:s');
 
      $board = [
        $request->input('username'),
        $request->input('comment'),
        $date,
      ];
 
      $file->fputcsv($board);
 
      // flashメッセージの設定
      session()->flash('success', '書き込みを追加しました。');
        
      return redirect('/simple_bbs');
    
    }
    
}

ビュー

resources/views/samples/index.blade.php

@extends('layouts.default')
 
@section('title', $title)
 
@section('content')
<h1>ひとこと掲示板</h1>
 
<form method="post" action="{{ url('/simple_bbs') }}">
  @csrf
  <div>
    <label>
      名前
      <input type="text" name="username">
    </label>
  </div>
  <div>
    <label>
      コメント
      <input type="text" name="comment">
    </label>
  </div>

  <div>
    <input type="submit" name="送信">
  </div>
</form>
 
  <ul>
      @foreach($boards as $board)
        <li>{{ $board[0] }}: {{ $board[1] }} [{{ $board[2] }}]</li>
      @endforeach
  </ul>
 
@endsection

共通レイアウトファイル

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>@yield('title')</title>
    <style>
        .header_nav {
            display: flex;
            list-style: none;
            padding-left: 0;
        }
        .header_nav li {
            margin-right: 30px;
        }
        /* エラーメッセージ用のスタイル */
        .error {
          color: red;
        }
        /* 成功メッセージ用のスタイル */
        .success {
          color: green;
        }
    </style>
</head>
<body>
    @yield('header')
 
    {{-- エラーメッセージを出力 --}}
    @foreach($errors->all() as $error)
      <p class="error">{{ $error }}</p>
    @endforeach
 
    {{-- 成功メッセージを出力 --}}
    @if (session()->has('success'))
        <div class="success">
            {{ session()->get('success') }}
        </div>
    @endif
 
    @yield('content')
</body>
</html>
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?