Laravel 项目:使用 TDD 构建论坛 Chapter 12

0.写在前面

  • 本系列文章为laracasts.com 的系列视频教程——Let's Build A Forum with Laravel and TDD 的学习笔记。若喜欢该系列视频,可去该网站订阅后下载该系列视频, 支持正版
  • 视频源码地址:https://github.com/laracasts/Lets-Build-a-Forum-in-Laravel
  • *本项目为一个 forum(论坛)项目,与本站的第二本实战教程 Laravel 教程 - Web 开发实战进阶 ( Laravel 5.5 ) 类似,可互相参照
  • 项目开发模式为TDD开发,教程简介为:

    A forum is a deceptively complex thing. Sure, it's made up of threads and replies, but what else might exist as part of a forum? What about profiles, or thread subscriptions, or filtering, or real-time notifications? As it turns out, a forum is the perfect project to stretch your programming muscles. In this series, we'll work together to build one with tests from A to Z.

  • 项目版本为 laravel 5.4,教程后面会进行升级到 laravel 5.5 的教学
  • 视频教程共计 102 个小节,笔记章节与视频教程一一对应

1.本节说明

  • 对应视频第 12 小节:Validation Errors And Old Data

2.本节内容

现在我们通过访问 http://forum.test/threads/create 可以创建新的话题,但我们在主页面无法进入到创建页面。让我们来加上这一功能:
forum\resources\views\layouts\app.blade.php

.
.
<div class="collapse navbar-collapse" id="app-navbar-collapse">
    <!-- Left Side Of Navbar -->
    <ul class="nav navbar-nav">
        <li><a href="/threads">All Threads</a></li>

        <li><a href="/threads/create">New Thread</a></li>  // 新建页面的链接
        .
        .

刷新页面:
file
现在我们可以点击New Thread进入创建页面:
file
但是如果我们尝试不合法的提交:
file
点击Publish按钮,会发现页面重新刷新:
file
因为不合法的提交被拒绝,页面重定向至create页面。接下来需要把上一次提交的内容和错误的消息显示出来:
forum\resources\views\threads\create.blade.php

.
.
<div class="panel-body">
    <form method="post" action="/threads">
        {{ csrf_field() }}

        <div class="form-group">
            <label for="title">Title</label>
            <input type="text" class="form-control" id="title" name="title" value="{{ old('title') }}">
        </div>

        <div class="form-group">
            <label for="body">Body</label>
            <textarea name="body" id="body" class="form-control" rows="8">{{ old('body') }}</textarea>
        </div>

        <div class="form-group">
            <button type="submit" class="btn btn-primary">Publish</button>
        </div>

        @if(count($errors))
            <ul class="alert alert-danger">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        @endif
    </form>

</div>
.
.

再次尝试不合法提交:
file
因为我们定义了channel_id为必填项,所以提交会不通过。修改创建页面:
forum\resources\views\threads\create.blade.php

 .
 .
 <div class="panel-body">
    <form method="post" action="/threads">
        {{ csrf_field() }}

        <div class="form-group">
            <label for="channel_id">Choose a Channel</label>
            <select name="channel_id" id="channel_id" class="form-control" required>
                <option value="">Choose One...</option>
                @foreach(App\Channel::all() as $channel)
                    <option value="{{ $channel->id }}" {{ old('channel_id') == $channel->id ? 'selected' : ''}}>
                        {{ $channel->name }}
                    </option>
                @endforeach
            </select>
        </div>

        <div class="form-group">
            <label for="title">Title</label>
            <input type="text" class="form-control" id="title" name="title" value="{{ old('title') }}" required>
        </div>

        <div class="form-group">
            <label for="body">Body</label>
            <textarea name="body" id="body" class="form-control" rows="8" required>{{ old('body') }}</textarea>
        </div>

        <div class="form-group">
            <button type="submit" class="btn btn-primary">Publish</button>
        </div>

        @if(count($errors))
            <ul class="alert alert-danger">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        @endif
    </form>

</div>
.
.

尝试提交:
file
提交成功:
file

3.写在后面

  • 如有建议或意见,欢迎指出~
  • 如果觉得文章写的不错,请点赞鼓励下哈,你的鼓励将是我的动力!
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!