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

Laravel导入URL JSON文件并实现基因搜索功能求助

Laravel导入基因JSON并实现搜索功能快速指南

1. 代码放置位置:控制器

首先创建专门处理基因逻辑的控制器:

php artisan make:controller GeneController

打开app/Http/Controllers/GeneController.php,写入完整的index方法(修正原代码语法错误,同时处理数据提取与搜索):

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

public function index(Request $request)
{
    // 缓存远程JSON数据,避免重复拉取大文件,缓存有效期1天(86400秒)
    $rawGenes = Cache::remember('protein_coding_genes', 86400, function () {
        $jsonContent = file_get_contents("http://ftp.ebi.ac.uk/pub/databases/genenames/hgnc/json/locus_groups/protein-coding_gene.json");
        return json_decode($jsonContent, true)['response']['docs'];
    });

    // 处理搜索过滤
    if ($request->has('search')) {
        $searchTerm = strtolower($request->input('search'));
        $rawGenes = array_filter($rawGenes, function ($gene) use ($searchTerm) {
            return str_contains(strtolower($gene['symbol']), $searchTerm) 
                || str_contains(strtolower($gene['location']), $searchTerm);
        });
    }

    // 只保留需要的字段:基因符号、位置
    $filteredGenes = array_map(function ($gene) {
        return [
            'gene_symbol' => $gene['symbol'],
            'location' => $gene['location']
        ];
    }, $rawGenes);

    return view('genes.index', compact('filteredGenes'));
}

2. 添加路由

在routes/web.php中注册访问路由:

Route::get('/genes', [App\Http\Controllers\GeneController::class, 'index'])->name('genes.index');

3. 创建前端视图

在resources/views下新建genes文件夹,创建index.blade.php文件,实现搜索表单与数据展示:

<!DOCTYPE html>
<html>
<head>
    <title>基因搜索</title>
    <style>
        .search-bar { margin: 2rem 0; }
        .search-bar input { padding: 0.5rem; width: 300px; }
        .search-bar button { padding: 0.5rem 1rem; }
        .gene-table { width: 100%; border-collapse: collapse; }
        .gene-table th, .gene-table td { border: 1px solid #eee; padding: 0.8rem; text-align: left; }
        .gene-table th { background-color: #f5f5f5; }
    </style>
</head>
<body>
    <div class="search-bar">
        <form action="{{ route('genes.index') }}" method="GET">
            <input type="text" name="search" placeholder="搜索基因符号或位置..." value="{{ request('search') }}">
            <button type="submit">搜索</button>
        </form>
    </div>

    <table class="gene-table">
        <thead>
            <tr>
                <th>基因符号</th>
                <th>位置</th>
            </tr>
        </thead>
        <tbody>
            @foreach($filteredGenes as $gene)
                <tr>
                    <td>{{ $gene['gene_symbol'] }}</td>
                    <td>{{ $gene['location'] }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>
</body>
</html>

4. 快速验证与调整

  • 启动Laravel服务:php artisan serve,访问http://localhost:8000/genes即可看到效果
  • 如果需要更新缓存数据,执行命令:php artisan cache:forget protein_coding_genes
  • 若搜索逻辑需要调整,直接修改控制器中的过滤条件即可

内容的提问来源于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.15 14:10:46