TYPO3 12集成SOLR:实现可读筛选器URL及搜索参数优化
问题解决:TYPO3 12 + SOLR 自定义筛选器URL标识与简化搜索参数
一、替换筛选器URL中的类型标识
当前你的SOLR索引中type字段存储的是数据表名tx_company_z_pressphotos,因此生成的筛选URL会默认使用该值。要替换为自定义标识myPressphotosLabel,需修改索引阶段的字段映射,而非仅调整前端渲染:
- 修改IndexQueue配置,覆盖type字段
在plugin.tx_solr.index.queue.pressphotos.fields中新增自定义type字段,将其值设为你的目标标识:
plugin.tx_solr.index.queue { pressphotos = 1 pressphotos { type = tx_company_z_pressphotos label = myPressphotosLabel fields { # 新增:覆盖默认type字段值为自定义标识 type = TEXT type.value = myPressphotosLabel # 保留原有字段配置 title = title datetime_stringS = TEXT datetime_stringS { field = record_date date = Y-m-d } content = SOLR_CONTENT content { cObject = COA cObject { 10 = TEXT 10 { field = text noTrimWrap = || | } } } url = TEXT url { typolink.parameter = {$pid.detail.pressphotos} typolink.additionalParams.insertData = 1 typolink.useCacheHash = 1 typolink.returnLast = url typolink.linkAccessRestrictedPages = 1 } } } }
更新SOLR索引
修改配置后,清空对应队列并重新索引记录,确保SOLR中type字段的值已更新为myPressphotosLabel。调整Facet配置(可选)
若需保留前端自定义标签显示,可保留原renderingInstruction但调整映射逻辑,此时生成的筛选URL会自动使用myPressphotosLabel作为参数值。
二、简化搜索参数tx_solr[q]
要将tx_solr[q]替换为更简洁的参数(如q),可通过以下两种方式实现:
方法1:TYPO3路由增强(推荐)
在站点config.yaml中添加路由规则,将简洁参数映射到SOLR原始参数:
routeEnhancers: SolrSearch: type: Extbase extension: Solr plugin: Search routes: - routePath: '/search/{q}' _controller: 'Search::results' _arguments: q: 'tx_solr[q]' defaultController: 'Search::results' defaults: q: '' requirements: q: '.+'
此配置支持两种访问形式:
- 路径形式:
/search/JohnDoe自动映射为tx_solr[q]=JohnDoe - GET参数形式:需配合中间件将
?q=JohnDoe转换为tx_solr[q]=JohnDoe
方法2:自定义中间件处理参数映射
创建中间件实现参数转换:
<?php namespace Vendor\MyExtension\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; class SolrParamMiddleware implements MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $queryParams = $request->getQueryParams(); if (isset($queryParams['q']) && !isset($queryParams['tx_solr']['q'])) { $queryParams['tx_solr']['q'] = $queryParams['q']; unset($queryParams['q']); $request = $request->withQueryParams($queryParams); } return $handler->handle($request); } }
在Configuration/Services.yaml注册中间件:
services: Vendor\MyExtension\Middleware\SolrParamMiddleware: tags: - name: middleware after: 'typo3/cms-frontend/site'
注意:使用上述方案时,需同步修改前端搜索表单的提交参数为
q,并更新生成搜索URL的逻辑(如用<f:uri.page additionalParams="{q: searchQuery}" />替代原tx_solr参数)。
内容的提问来源于stack exchange,提问作者TZP
相关产品推荐
相关产品推荐

