Browse Source

🐛 (library): 文集阅读页与 blog 子页面必然 500

staging 日志(2026-11-19)里的两类确定性错误:

- 文集阅读页 /library/anthology/{a}/read/{article}:AnthologyReadController 渲染
  library.book.read 时没给 book_title,视图无条件读它 → Undefined array key。
  补上,取文集标题
- blog 的 categories / archives / archives/{year} / tag / search / {post}:路由指向
  BlogController 里被整段注释掉的方法 → BadMethodCallException。blog 布局仍用这些
  路由名生成导航链接,所以保留路由名、先回 404

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
visuddhinanda 10 hours ago
parent
commit
c76a35e5ff

+ 2 - 0
api-v13/app/Http/Controllers/Library/AnthologyReadController.php

@@ -113,6 +113,8 @@ class AnthologyReadController extends Controller
                 'id' => $anthologyId,
                 'title' => $col['title'] ?? '',
             ],
+            // 目录栏标题(book.read 视图无条件读取):文集阅读页的「书」就是文集本身
+            'book_title' => $col['title'] ?? '',
             'categories' => [],
             'tags' => [],
             'downloads' => [],

+ 12 - 6
api-v13/routes/web.php

@@ -111,14 +111,20 @@ Route::prefix('library')->name('library.')->group(function () {
 });
 // 博客路由
 Route::prefix('blog')->group(function () {
+    // categories / archives / tag / search / show 在 BlogController 里整段注释掉了(尚未实现),
+    // 但 blog 布局仍用这些路由名生成导航链接,所以路由名保留、先回 404——
+    // 指向不存在的控制器方法会抛 BadMethodCallException 变成 500(staging 日志里爬虫扫到过)。
+    // 实现了哪个就把它改回 [BlogController::class, '方法名']。
+    $notImplemented = fn () => abort(404);
+
     Route::get('/{user}', [BlogController::class, 'index'])->name('blog.index')->where('user', '[a-zA-Z0-9_-]+');
-    Route::get('/{user}/categories', [BlogController::class, 'categories'])->name('blog.categories');
+    Route::get('/{user}/categories', $notImplemented)->name('blog.categories');
     Route::get('/{user}/category/{category1}/{category2?}/{category3?}/{category4?}/{category5?}', [BlogController::class, 'category'])->name('blog.category');
-    Route::get('/{user}/archives', [BlogController::class, 'archives'])->name('blog.archives');
-    Route::get('/{user}/archives/{year}', [BlogController::class, 'archivesByYear'])->name('blog.archives.year');
-    Route::get('/{user}/tag/{tag}', [BlogController::class, 'tag'])->name('blog.tag');
-    Route::get('/{user}/search', [BlogController::class, 'search'])->name('blog.search');
-    Route::get('/{user}/{post}', [BlogController::class, 'show'])
+    Route::get('/{user}/archives', $notImplemented)->name('blog.archives');
+    Route::get('/{user}/archives/{year}', $notImplemented)->name('blog.archives.year');
+    Route::get('/{user}/tag/{tag}', $notImplemented)->name('blog.tag');
+    Route::get('/{user}/search', $notImplemented)->name('blog.search');
+    Route::get('/{user}/{post}', $notImplemented)
         ->name('blog.show')
         ->where([
             'user' => '[a-zA-Z0-9_-]+',

+ 74 - 0
api-v13/tests/Feature/LibraryPageErrorsTest.php

@@ -0,0 +1,74 @@
+<?php
+
+use App\Models\Article;
+use App\Models\Collection;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Str;
+
+/*
+ * staging 日志(2026-11-19)里两类必然 500 的 web 页面的回归测试:
+ * - blog 的子页面指向被注释掉的 BlogController 方法 → BadMethodCallException
+ * - 文集阅读页没给 book.read 视图 book_title → Undefined array key
+ */
+
+uses(RefreshDatabase::class);
+
+it('answers 404 instead of 500 for blog pages that are not implemented yet', function (string $path) {
+    makeStudio('blogger');
+
+    $this->get("/blog/blogger/{$path}")->assertNotFound();
+})->with([
+    'categories' => 'categories',
+    'archives' => 'archives',
+    'archives by year' => 'archives/2024',
+    'tag' => 'tag/dukkha',
+    'search' => 'search?q=dukkha',
+    'post' => 'some-post',
+]);
+
+it('still renders the blog home, whose layout links to those routes by name', function () {
+    makeStudio('blogger');
+
+    $this->get('/blog/blogger')
+        ->assertSuccessful()
+        ->assertSee(route('blog.archives', ['user' => 'blogger'], false))
+        ->assertSee(route('blog.search', ['user' => 'blogger'], false));
+});
+
+it('renders an anthology article with the anthology title as the book title', function () {
+    $owner = makeStudio('editor');
+    // ArticleResource 渲染时取作者名下第一个译文 channel,没有就报错(v2 代码的另一个问题,不在此修)
+    makeChannel($owner, 'editor translation');
+    $articleId = (string) Str::uuid();
+    $anthologyId = (string) Str::uuid();
+    $now = time() * 1000;
+
+    (new Article)->forceFill([
+        'id' => random_int(1, PHP_INT_MAX),
+        'uid' => $articleId,
+        'title' => '第一单元',
+        'content' => '正文',
+        'owner' => $owner,
+        'owner_id' => 0,
+        'editor_id' => 0,
+        'status' => 30,
+        'create_time' => $now,
+        'modify_time' => $now,
+    ])->save();
+    (new Collection)->forceFill([
+        'id' => random_int(1, PHP_INT_MAX),
+        'uid' => $anthologyId,
+        'title' => '初级班课本',
+        'article_list' => json_encode([['article_id' => $articleId, 'level' => 1, 'title' => '第一单元']]),
+        'owner' => $owner,
+        'owner_id' => 0,
+        'editor_id' => 0,
+        'status' => 30,
+        'create_time' => $now,
+        'modify_time' => $now,
+    ])->save();
+
+    $this->get("/library/anthology/{$anthologyId}/read/{$articleId}")
+        ->assertSuccessful()
+        ->assertSee('<h5 class="offcanvas-title">初级班课本</h5>', false);
+});