dd/app/Http/Controllers/Web/BirdController.php
2026-06-14 15:46:45 +08:00

67 lines
1.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers\Web;
use App\Models\Bird;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;
class BirdController
{
/**
* 显示鸟类档案列表(大厂工业规范版)
* 谐音发音:因代克斯 (Index) -> 意为索引、列表
*/
public function index(): \Illuminate\View\View
{
// 工业标准:按创建时间倒序排列,每页严格限制 10 条数据
// 匈牙利命名法c前缀代表集合(Collection)
$cBirdList = \App\Models\Bird::latest()->paginate(10);
// 渲染列表模板,把数据送过去
return view('bird.index', compact('cBirdList'));
}
/**
* 1. 显示新建鸟类档案的界面
*/
public function create(): View
{
return view('bird.create');
}
/**
* 2. 保存鸟类数据 (就是你刚才弄好的那个方法,保持原样)
*/
public function store(Request $oRequest): JsonResponse
{
$aValidated = $oRequest->validate([
'title' => 'required|string|max:255',
'markdown_content' => 'required|string',
]);
$oBird = Bird::create($aValidated);
return response()->json([
'status' => 'success',
'data' => [
'id' => $oBird->id
]
], 201);
}
/**
* 3. 显示鸟类档案详情界面(演示渲染出来的 Markdown
*/
public function show(int $iId): View
{
// 遵循大厂防御性编程:找不到直接抛出 404
// 谐音发音:凡得 欧尔 飞儿 (Find or Fail)
$oBird = Bird::findOrFail($iId);
// 传给 Blade 模板
return view('bird.show', compact('oBird'));
}
}