Skip to content

Commit

Permalink
用户可以创建话题
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Sep 20, 2019
1 parent e2ffb2e commit 647074a
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 94 deletions.
24 changes: 15 additions & 9 deletions app/Http/Controllers/TopicsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\TopicRequest;
use App\Models\Category;
use Auth;

class TopicsController extends Controller
{
Expand All @@ -25,16 +27,20 @@ public function show(Topic $topic)
return view('topics.show', compact('topic'));
}

public function create(Topic $topic)
{
return view('topics.create_and_edit', compact('topic'));
}
public function create(Topic $topic)
{
$categories = Category::all();
return view('topics.create_and_edit', compact('topic', 'categories'));
}

public function store(TopicRequest $request)
{
$topic = Topic::create($request->all());
return redirect()->route('topics.show', $topic->id)->with('message', 'Created successfully.');
}
public function store(TopicRequest $request, Topic $topic)
{
$topic->fill($request->all());
$topic->user_id = Auth::id();
$topic->save();

return redirect()->route('topics.show', $topic->id)->with('success', '帖子创建成功!');
}

public function edit(Topic $topic)
{
Expand Down
14 changes: 6 additions & 8 deletions app/Http/Requests/TopicRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,30 @@ public function rules()
{
// CREATE
case 'POST':
{
return [
// CREATE ROLES
];
}
// UPDATE
case 'PUT':
case 'PATCH':
{
return [
// UPDATE ROLES
'title' => 'required|min:2',
'body' => 'required|min:3',
'category_id' => 'required|numeric',
];
}
case 'GET':
case 'DELETE':
default:
{
return [];
}
};
}
}

public function messages()
{
return [
// Validation messages
'title.min' => '标题必须至少两个字符',
'body.min' => '文章内容必须至少三个字符',
];
}
}
3 changes: 1 addition & 2 deletions app/Models/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
class Topic extends Model
{
protected $fillable = [
'title', 'body', 'user_id', 'category_id', 'reply_count',
'view_count', 'last_reply_user_id', 'order', 'excerpt', 'slug',
'title', 'body', 'category_id', 'excerpt', 'slug'
];

public function category()
Expand Down
11 changes: 3 additions & 8 deletions app/Observers/TopicObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@

class TopicObserver
{
public function creating(Topic $topic)
public function saving(Topic $topic)
{
//
$topic->excerpt = make_excerpt($topic->body);
}

public function updating(Topic $topic)
{
//
}
}
}
6 changes: 6 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ function category_nav_active($category_id)
{
return active_class((if_route('categories.show') && if_route_param('category', $category_id)));
}

function make_excerpt($value, $length = 200)
{
$excerpt = trim(preg_replace('/\r\n|\r|\n+/', ' ', strip_tags($value)));
return str_limit($excerpt, $length);
}
5 changes: 5 additions & 0 deletions resources/views/layouts/_header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<li class="nav-item"><a class="nav-link" href="{{ route('login') }}">登录</a></li>
<li class="nav-item"><a class="nav-link" href="{{ route('register') }}">注册</a></li>
@else
<li class="nav-item">
<a class="nav-link mt-1 mr-3 font-weight-bold" href="{{ route('topics.create') }}">
<i class="fa fa-plus"></i>
</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<img src="{{ Auth::user()->avatar }}" class="img-responsive img-circle" width="30px" height="30px">
Expand Down
4 changes: 3 additions & 1 deletion resources/views/topics/_sidebar.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<div class="card ">
<div class="card-body">
右边导航栏
<a href="{{ route('topics.create') }}" class="btn btn-success btn-block" aria-label="Left Align">
<i class="fas fa-pencil-alt mr-2"></i> 新建帖子
</a>
</div>
</div>
106 changes: 40 additions & 66 deletions resources/views/topics/create_and_edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,57 @@

@section('content')

<div class="container">
<div class="col-md-10 offset-md-1">
<div class="card ">
<div class="container">
<div class="col-md-10 offset-md-1">
<div class="card ">

<div class="card-body">
<h2 class="">
<i class="far fa-edit"></i>
@if($topic->id)
编辑话题
@else
新建话题
@endif
</h2>

<hr>

<div class="card-header">
<h1>
Topic /
@if($topic->id)
Edit #{{ $topic->id }}
<form action="{{ route('topics.update', $topic->id) }}" method="POST" accept-charset="UTF-8">
<input type="hidden" name="_method" value="PUT">
@else
Create
<form action="{{ route('topics.store') }}" method="POST" accept-charset="UTF-8">
@endif
</h1>
</div>

<div class="card-body">
@if($topic->id)
<form action="{{ route('topics.update', $topic->id) }}" method="POST" accept-charset="UTF-8">
<input type="hidden" name="_method" value="PUT">
@else
<form action="{{ route('topics.store') }}" method="POST" accept-charset="UTF-8">
@endif
<input type="hidden" name="_token" value="{{ csrf_token() }}">

@include('shared._error')

@include('common.error')
<div class="form-group">
<input class="form-control" type="text" name="title" value="{{ old('title', $topic->title ) }}" placeholder="请填写标题" required />
</div>

<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<select class="form-control" name="category_id" required>
<option value="" hidden disabled selected>请选择分类</option>
@foreach ($categories as $value)
<option value="{{ $value->id }}">{{ $value->name }}</option>
@endforeach
</select>
</div>


<div class="form-group">
<label for="title-field">Title</label>
<input class="form-control" type="text" name="title" id="title-field" value="{{ old('title', $topic->title ) }}" />
</div>
<div class="form-group">
<label for="body-field">Body</label>
<textarea name="body" id="body-field" class="form-control" rows="3">{{ old('body', $topic->body ) }}</textarea>
</div>
<div class="form-group">
<label for="user_id-field">User_id</label>
<input class="form-control" type="text" name="user_id" id="user_id-field" value="{{ old('user_id', $topic->user_id ) }}" />
</div>
<div class="form-group">
<label for="category_id-field">Category_id</label>
<input class="form-control" type="text" name="category_id" id="category_id-field" value="{{ old('category_id', $topic->category_id ) }}" />
</div>
<div class="form-group">
<label for="reply_count-field">Reply_count</label>
<input class="form-control" type="text" name="reply_count" id="reply_count-field" value="{{ old('reply_count', $topic->reply_count ) }}" />
</div>
<div class="form-group">
<label for="view_count-field">View_count</label>
<input class="form-control" type="text" name="view_count" id="view_count-field" value="{{ old('view_count', $topic->view_count ) }}" />
</div>
<div class="form-group">
<label for="last_reply_user_id-field">Last_reply_user_id</label>
<input class="form-control" type="text" name="last_reply_user_id" id="last_reply_user_id-field" value="{{ old('last_reply_user_id', $topic->last_reply_user_id ) }}" />
</div>
<div class="form-group">
<label for="order-field">Order</label>
<input class="form-control" type="text" name="order" id="order-field" value="{{ old('order', $topic->order ) }}" />
</div>
<div class="form-group">
<label for="excerpt-field">Excerpt</label>
<textarea name="excerpt" id="excerpt-field" class="form-control" rows="3">{{ old('excerpt', $topic->excerpt ) }}</textarea>
</div>
<div class="form-group">
<label for="slug-field">Slug</label>
<input class="form-control" type="text" name="slug" id="slug-field" value="{{ old('slug', $topic->slug ) }}" />
</div>
<div class="form-group">
<textarea name="body" class="form-control" id="editor" rows="6" placeholder="请填入至少三个字符的内容。" required>{{ old('body', $topic->body ) }}</textarea>
</div>

<div class="well well-sm">
<button type="submit" class="btn btn-primary">Save</button>
<a class="btn btn-link float-xs-right" href="{{ route('topics.index') }}"> <- Back</a>
</div>
</form>
<div class="well well-sm">
<button type="submit" class="btn btn-primary"><i class="far fa-save mr-2" aria-hidden="true"></i> 保存</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>

@endsection

0 comments on commit 647074a

Please sign in to comment.