62.基本视图调整
- 本系列文章为
laracasts.com
的系列视频教程 ——Let's Build A Forum with Laravel and TDD 的学习笔记。若喜欢该系列视频,可去该网站订阅后下载该系列视频, 支持正版 ;- 视频源码地址:github.com/laracasts/Lets-Build-a-...;
- 本项目为一个 forum(论坛)项目,与本站的第二本实战教程 《Laravel 教程 - Web 开发实战进阶》 类似,可互相参照。
本节说明#
- 对应视频教程第 62 小节:Basic View Tweaks
本节内容#
本节让我们花点时间来进行一些较小的调整。例如,在话题列表页面上,我们应该显示作者的名字。另外,我们还没有在这里使用分页。现在我们来解决这两个问题。我们将话题列表抽取成单独的视图文件,并加上作者:
forum\resources\views\threads\ _list.blade.php
@forelse ($threads as $thread)
<div class="panel panel-default">
<div class="panel-heading">
<div class="level">
<div class="flex">
<h4>
<a href="{{ $thread->path() }}">
@if(auth()->check() && $thread->hasUpdatesFor(auth()->user()))
<strong>
{{ $thread->title }}
</strong>
@else
{{ $thread->title }}
@endif
</a>
</h4>
<h5>
Posted By:<a href="{{ route('profile',$thread->creator) }}">{{ $thread->creator->name }}</a>
</h5>
</div>
<a href="{{ $thread->path() }}">
{{ $thread->replies_count }} {{ str_plural('reply',$thread->replies_count) }}
</a>
</div>
</div>
<div class="panel-body">
<div class="body">{{ $thread->body }}</div>
</div>
</div>
@empty
<p>There are no relevant results at this time.</p>
@endforelse
然后引入视图:
forum\resources\views\threads\index.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
@include ('threads._list')
</div>
</div>
</div>
@endsection
看一下页面效果:
下一步我们加上分页功能:
forum\app\Http\Controllers\ThreadsController.php
.
.
protected function getThreads(Channel $channel, ThreadsFilters $filters)
{
$threads = Thread::with('channel')->latest()->filter($filters);
if ($channel->exists) {
$threads->where('channel_id', $channel->id);
}
$threads = $threads->paginate(20);
return $threads;
}
}
在页面加上链接:
forum\resources\views\threads\index.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
@include ('threads._list')
{{ $threads->render() }}
</div>
</div>
</div>
@endsection
页面效果:
我们来运行一下测试:
有两个测试未通过,我们来修复它们。我们先打印出 $response
的值:
我们使用了分页函数,所以返回数据的内容发生了改变。我们简单修复即可:
forum\tests\Feature\ReadThreadsTest.php
.
.
/** @test */
public function a_user_can_filter_threads_by_popularity()
{
// Given we have three threads
// With 2 replies,3 replies,0 replies, respectively
$threadWithTwoReplies = create('App\Thread');
create('App\Reply',['thread_id'=>$threadWithTwoReplies->id],2);
$threadWithThreeReplies = create('App\Thread');
create('App\Reply',['thread_id'=>$threadWithThreeReplies->id],3);
$threadWithNoReplies = $this->thread;
// When I filter all threads by popularity
$response = $this->getJson('threads?popularity=1')->json();
// Then they should be returned from most replies to least.
$this->assertEquals([3,2,0],array_column($response['data'],'replies_count'));
}
/** @test */
public function a_user_can_filter_threads_by_those_that_are_unanswered()
{
$thread = create('App\Thread');
create('App\Reply',['thread_id' => $thread->id]);
$response = $this->getJson('threads?unanswered=1')->json();
$this->assertCount(1,$response['data']);
}
.
.
再次运行测试:
推荐文章: