Statamic 3.3升级至4.5后子条目URL路径合并异常求助
解决Statamic 4.5中子条目URL层级路径失效问题
1. 检查集合路由配置
- 进入后台集合设置(Collections -> 目标集合 -> Settings),确认Route模板设置为包含父条目slug的格式,比如
{{ parent:slug }}/{{ slug }},而非仅{{ slug }}。 - 若通过配置文件管理集合(
config/statamic/collections.php),对应集合添加或修改route字段:'route' => '{{ parent:slug }}/{{ slug }}',
2. 强制Eloquent Driver生成层级URI
由于使用Eloquent Driver,需确保条目保存时生成完整层级路径,可通过事件监听实现:
- 在
App\Providers\EventServiceProvider中注册事件监听:protected $listen = [ \Statamic\Events\EntrySaving::class => [ \App\Listeners\GenerateHierarchicalUri::class, ], ]; - 创建监听类
App\Listeners\GenerateHierarchicalUri:<?php namespace App\Listeners; use Statamic\Events\EntrySaving; class GenerateHierarchicalUri { public function handle(EntrySaving $event) { $entry = $event->entry; if ($parent = $entry->parent()) { $entry->setUri(rtrim($parent->uri(), '/') . '/' . $entry->slug()); } } }
3. 修复已有条目并刷新缓存
- 直接在数据库中更新子条目
uri字段为完整路径(如/home/test)。 - 执行命令刷新路由缓存:
php artisan statamic:clear:routes php artisan route:cache
4. 禁用自动扁平化URI设置
- 检查集合设置中的URI Generation选项,确保未勾选“Flatten URI”相关选项。
- 配置文件中对应集合设置
flat为false:'flat' => false,
内容的提问来源于stack exchange,提问作者Matthew Bradley
相关产品推荐
相关产品推荐

