You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel基因搜索功能报错:路由未定义与语法错误求助

基因搜索功能错误修复方案

1. 修复「route 'search' not defined」错误

你的搜索路由未设置名称,导致route('search')无法匹配对应路由。同时路由文件缺少SearchController的引入,需一并修正:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SiteController;
use App\Http\Controllers\SearchController; // 补上控制器引入

Route::get('/data', [SiteController::class, 'index']);
// 给路由添加名称,解决route()调用报错
Route::get('search', [SearchController::class, 'search'])->name('search');
Route::get('/', function () {
    return view('welcome');
});

2. 修复「syntax error unexpected token "if"」错误

该错误源于Blade语法与原生PHP标签混用,同时存在多余符号、变量重定义等问题,修改welcome.blade.php相关代码:

<!-- 修正表单标签(移除多余的>) -->
<form action="{{ route('search') }}" method="GET">
    <input type="search" class="form-control" placeholder="Search Gene Name" name="search">
    <button type="submit">Search</button>
</form>

<!-- 修正结果展示逻辑 -->
<h2>Results <i class="fa-solid fa-square-poll-horizontal"></i></h2>
@if(isset($filtered) && $filtered->isNotEmpty())
    @foreach ($filtered as $gene)
        <ul class="list-group mt-3">
            <li class="list-group-item">{{ $gene['name'] }}</li>
            <li class="list-group-item">{{ $gene['location'] }}</li>
            <li class="list-group-item">{{ $gene['symbol'] }}</li>
        </ul>
    @endforeach
@else 
    <div>
        <h2>No Gene found</h2>
    </div>
@endif

修正点说明:

  • 移除包裹Blade指令的<?php ?>标签,Blade指令无需原生PHP包裹
  • 移除表单标签末尾多余的>
  • 循环变量重命名为$gene,避免与数据集变量冲突
  • 增加isset($filtered)判断,防止初始访问时变量未定义报错
  • 调整变量调用方式为数组索引(后续搜索逻辑返回数组格式)

3. 修正SearchController逻辑错误

原代码使用Eloquent模型查询语法,但你的数据来自远程JSON文件而非数据库,需修改为内存搜索逻辑:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class SearchController extends Controller
{
    public function search(Request $request){
        // 获取搜索关键词,默认空字符串
        $search = $request->input('search', '');

        // 加载远程JSON数据(建议后续改为缓存,避免重复请求)
        $jsonContent = file_get_contents("http://ftp.ebi.ac.uk/pub/databases/genenames/hgnc/json/locus_groups/protein-coding_gene.json");
        $rawData = json_decode($jsonContent, true);
        
        // 提取JSON中的基因列表(对应JSON结构里的response->docs节点)
        $genes = $rawData['response']['docs'] ?? [];
        
        // 过滤匹配关键词的基因(支持名称和符号模糊搜索)
        $filtered = collect($genes)->filter(function($item) use ($search) {
            return stripos($item['name'], $search) !== false 
                || stripos($item['symbol'], $search) !== false;
        });

        // 返回welcome视图,携带过滤后的结果
        return view('welcome', compact('filtered'));
    }
}

修正点说明:

  • 直接加载远程JSON数据,解析为数组后提取有效基因列表
  • 使用Collection的filter方法实现内存搜索,支持名称和符号的模糊匹配
  • 返回结果到welcome视图(与搜索表单同页面),而非不存在的search视图

4. 额外优化建议

  • 远程JSON文件体积较大,建议将数据缓存到本地文件或Laravel缓存系统,避免每次搜索重复请求
  • 给搜索输入添加验证规则,比如$request->validate(['search' => 'required|string|min:1']);
  • SiteController中的dd($data)仅用于测试,正式环境需移除或改为返回数据视图

内容的提问来源于stack exchange,提问作者Nicole Jaffke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 14:40:28