-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostController.php
122 lines (104 loc) · 3.28 KB
/
PostController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Model\Post;
use App\Http\Model\PostCategory;
use Validator;
use Illuminate\Support\Facades\Input;
use Request;
class PostController extends CommonController
{
public function __construct()
{
$this->cat = array('news');
$this->catName = array(
'news' => '文章',
);
$this->page = Request::segment(2);//取得網址片段
}
//get. admin/news 全部分類列表
public function index()
{
$data = Post::where('cat', $this->page)->orderBy('id', 'desc')->paginate(10);
$catName = $this->catName[$this->page];
return view('admin.post.news.index', compact('data', 'catName'));
}
//get. admin/news/create 新增分類
public function create()
{
$category = (new PostCategory)->tree($this->page);
$catName = $this->catName[$this->page];
return view('admin.post.news.create', compact('category', 'catName'));
}
//post. admin/news 新增文章提交
public function store()
{
$input = Input::except('_token', 'file');
$input['cat'] = $this->page;
$input['pic'] = $this->upload();
$input['created_date'] = time();
$rules =[
'name' => 'required',
'content' => 'required',
];
$message =[
'name.required' => '文章標題不能為空',
'content.required' => '文章內容不能為空',
];
$validator = Validator::make($input,$rules,$message);
if($validator->passes()){
$re = Post::create($input);
if($re){
return redirect('admin/news');
}else{
return back()->with('msg','新增失敗,請稍後重試');
}
}else{
return back()->withErrors($validator);
}
}
//get. admin/news/{id}/edit 編輯文章
public function edit($id)
{
$category = (new PostCategory)->tree($this->page);
$catName = $this->catName[$this->page];
$field = Post::find($id);
return view('admin.post.news.edit', compact('category', 'field' ,'catName'));
}
//put. admin/news/{id} 更新文章
public function update($id)
{
$input = Input::except('_token','_method', 'file');
$pic = $this->upload();
$upload = "";
if($pic != 'fail'){
$upload = Post::where('id', $id)->update(['pic' => $pic]);
}
$re = Post::where('id', $id)->update($input);
if($re || $upload){
return redirect('admin/news')->with('msg', '文章修改成功!');
}else{
return back()->with('msg', '文章修改失敗,請稍後重試!');
}
}
//get. admin/news/{id} 顯示單個分類訊息
public function show()
{
}
//get. admin/news/{id} 刪除單個文章
public function destroy($id)
{
$re = Post::where('id', $id)->delete();
if($re){
$data = [
'status' => 0,
'msg' =>'文章刪除成功!',
];
}else{
$data = [
'status' => 1,
'msg' =>'文章刪除失敗,請稍後重試!',
];
}
return $data;
}
}