初始化
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Article;
|
||||
use App\Models\Category;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ArticleController extends Controller
|
||||
{
|
||||
public function index(): View
|
||||
{
|
||||
$articles = Article::with('category')->orderByDesc('created_at')->paginate(20);
|
||||
|
||||
return view('admin.articles.index', compact('articles'));
|
||||
}
|
||||
|
||||
public function create(): View
|
||||
{
|
||||
$categories = Category::ofType('article')->orderBy('sort')->orderBy('name')->get();
|
||||
|
||||
return view('admin.articles.form', ['article' => null, 'categories' => $categories]);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'category_id' => ['nullable', 'exists:categories,id'],
|
||||
'title' => ['required', 'string', 'max:160'],
|
||||
'slug' => ['required', 'string', 'max:160', 'unique:articles,slug'],
|
||||
'summary' => ['nullable', 'string', 'max:255'],
|
||||
'body' => ['nullable', 'string'],
|
||||
'cover' => ['nullable', 'string', 'max:255'],
|
||||
'author' => ['nullable', 'string', 'max:60'],
|
||||
'status' => ['required', 'in:approved,draft'],
|
||||
'template' => ['required', 'in:standard,tech,feature'],
|
||||
'seo_title' => ['nullable', 'string', 'max:160'],
|
||||
'seo_description' => ['nullable', 'string', 'max:255'],
|
||||
'published_at' => ['nullable', 'date'],
|
||||
]);
|
||||
|
||||
if (! empty($data['body'])) {
|
||||
$data['body'] = \Purifier::clean($data['body'], 'article');
|
||||
}
|
||||
|
||||
if (empty($data['published_at']) && $data['status'] === 'approved') {
|
||||
$data['published_at'] = now();
|
||||
}
|
||||
|
||||
Article::create($data);
|
||||
|
||||
return redirect()->route('admin.articles.index')->with('success', '文章已创建');
|
||||
}
|
||||
|
||||
public function edit(Article $article): View
|
||||
{
|
||||
$categories = Category::ofType('article')->orderBy('sort')->orderBy('name')->get();
|
||||
|
||||
return view('admin.articles.form', compact('article', 'categories'));
|
||||
}
|
||||
|
||||
public function update(Request $request, Article $article)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'category_id' => ['nullable', 'exists:categories,id'],
|
||||
'title' => ['required', 'string', 'max:160'],
|
||||
'slug' => ['required', 'string', 'max:160', 'unique:articles,slug,' . $article->id],
|
||||
'summary' => ['nullable', 'string', 'max:255'],
|
||||
'body' => ['nullable', 'string'],
|
||||
'cover' => ['nullable', 'string', 'max:255'],
|
||||
'author' => ['nullable', 'string', 'max:60'],
|
||||
'status' => ['required', 'in:approved,draft'],
|
||||
'template' => ['required', 'in:standard,tech,feature'],
|
||||
'seo_title' => ['nullable', 'string', 'max:160'],
|
||||
'seo_description' => ['nullable', 'string', 'max:255'],
|
||||
'published_at' => ['nullable', 'date'],
|
||||
]);
|
||||
|
||||
if (! empty($data['body'])) {
|
||||
$data['body'] = \Purifier::clean($data['body'], 'article');
|
||||
}
|
||||
|
||||
if (empty($data['published_at']) && $data['status'] === 'approved' && $article->published_at === null) {
|
||||
$data['published_at'] = now();
|
||||
}
|
||||
|
||||
$article->update($data);
|
||||
|
||||
return redirect()->route('admin.articles.index')->with('success', '文章已更新');
|
||||
}
|
||||
|
||||
public function destroy(Article $article)
|
||||
{
|
||||
$article->delete();
|
||||
|
||||
return redirect()->route('admin.articles.index')->with('success', '文章已删除');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user