laravel-learn-bbs/app/Http/Controllers/TopicsController.php

104 lines
3.1 KiB
PHP
Raw Normal View History

2018-01-01 04:00:45 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\TopicRequest;
2018-01-01 08:32:20 +00:00
use App\Models\Category;
2018-06-02 13:48:16 +00:00
use App\Models\Link;
2018-01-01 04:00:45 +00:00
use App\Models\Topic;
2018-06-02 10:21:53 +00:00
use App\Models\User;
2018-01-01 10:48:03 +00:00
use App\Tools\ImageUploadTool;
2018-01-01 07:22:14 +00:00
use Illuminate\Http\Request;
2018-01-01 08:32:20 +00:00
use Illuminate\Support\Facades\Auth;
2018-01-01 04:00:45 +00:00
class TopicsController extends Controller
{
public function __construct()
{
$this->middleware('auth', ['except' => ['index', 'show']]);
}
2018-01-01 08:32:20 +00:00
public function create(Topic $topic)
{
$categories = Category::all();
return view('topics.create_and_edit', compact('topic', 'categories'));
}
2018-06-02 13:48:16 +00:00
public function index(Request $request, Topic $topic, User $user, Link $link)
2018-01-01 04:00:45 +00:00
{
2018-06-02 10:21:53 +00:00
$active_users = $user->getActiveUsers();
2018-01-01 07:22:14 +00:00
$topics = $topic->with('user', 'category')->withOrder($request->order)->paginate();
2018-06-02 13:48:16 +00:00
$links = $link->getAllCached();
return view('topics.index', compact('topics', 'links', 'active_users'));
2018-01-01 04:00:45 +00:00
}
2018-01-01 13:22:16 +00:00
public function show(Topic $topic, Request $request)
2018-01-01 04:00:45 +00:00
{
2018-01-01 13:22:16 +00:00
if (!empty($topic->slug) && $topic->slug != $request->slug) {
return redirect($topic->link(), 301);
}
2018-01-15 15:24:11 +00:00
$replies = $topic->replies;
return view('topics.show', compact('topic', 'replies'));
2018-01-01 04:00:45 +00:00
}
2018-01-01 08:32:20 +00:00
public function store(TopicRequest $request, Topic $topic)
2018-01-01 04:00:45 +00:00
{
2018-01-01 08:32:20 +00:00
$topic->fill($request->all());
$topic->user_id = Auth::id();
$topic->save();
2018-01-01 13:22:16 +00:00
return redirect()->to($topic->link())->with('success', '添加成功!.');
2018-01-01 04:00:45 +00:00
}
public function edit(Topic $topic)
{
$this->authorize('update', $topic);
2018-01-01 10:48:03 +00:00
$categories = Category::all();
return view('topics.create_and_edit', compact('topic', 'categories'));
}
public function uploadImage(Request $request, ImageUploadTool $imageUploadTool)
{
// 初始化返回数据,默认是失败的
$data = [
'success' => false,
'msg' => '上传失败!',
'file_path' => ''
];
// 判断是否有上传文件,并赋值给 $file
if ($file = $request->upload_file) {
// 保存图片到本地
$result = $imageUploadTool->save($request->upload_file, 'topics', \Auth::id(), 1024);
// 图片保存成功的话
if ($result) {
$data['file_path'] = $result['path'];
$data['msg'] = "上传成功!";
$data['success'] = true;
}
}
return $data;
2018-01-01 04:00:45 +00:00
}
public function update(TopicRequest $request, Topic $topic)
{
2018-06-02 08:28:28 +00:00
try {
$this->authorize('update', $topic);
$topic->update($request->all());
} catch (\Exception $exception) {
echo $exception->getMessage();
}
2018-01-01 04:00:45 +00:00
2018-01-01 13:22:16 +00:00
return redirect()->route('topics.show', [$topic->id, $topic->slug])->with('success', '编辑成功!');
2018-01-01 04:00:45 +00:00
}
public function destroy(Topic $topic)
{
2018-06-02 08:28:28 +00:00
try {
$this->authorize('destroy', $topic);
$topic->delete();
} catch (\Exception $exception) {
echo $exception->getMessage();
}
2018-01-01 13:22:16 +00:00
return redirect()->route('topics.index')->with('message', '删除成功.');
2018-01-01 04:00:45 +00:00
}
}