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

Lando环境中Laravel基础功能测试get('/')请求失败排查

Lando环境中Laravel功能测试请求URL错误导致404的解决方法

在本地Lando环境运行Laravel应用的功能测试,应用可通过catalog.lndo.site访问。执行示例测试代码(调用get('/')并断言状态码200),使用命令lando php artisan test --testsuite=Feature时测试失败,返回404状态码。跟踪发现请求URL错误地变成了GET /catalog.lndo.site HTTP/1.1。

示例测试代码

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function test_example()
    {
        $response = $this->get('/');
        $response->assertStatus(200);
    }
}

错误信息

• Tests\Feature\ExampleTest > example
  Expected response status code [200] but received 404.
  Failed asserting that 200 is identical to 404.

  at tests/Feature/ExampleTest.php:18
     14▕      */
     15▕     public function test_example()
     16▕     {
     17▕         $response = $this->get('/');
  ➜  18▕         $response->assertStatus(200);
     19▕     }
     20▕ }
     21▕ 


  Tests:  1 failed
  Time:   0.66s

请求信息

GET /catalog.lndo.site HTTP/1.1
Accept:          text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Language: en-us,en;q=0.5
Host:            localhost
User-Agent:      Symfony

.env配置

APP_NAME=Laravel
APP_ENV=local
DUSK_DRIVER_URL=http://chromedriver:4444/wd/hub
APP_DEBUG=true
APP_URL=catalog.lndo.site
APP_KEY=base64:VsA/HpgsJT1somemorestring=
DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=laravel

解决方案

1. 配置测试环境专属的.env文件

创建或修改.env.testing文件,设置测试环境的APP_URL为http://localhost,避免测试时把域名解析为路径:

APP_ENV=testing
APP_URL=http://localhost
APP_DEBUG=true
# 其他必要配置可从.env复制,比如数据库连接信息

Laravel在运行测试时会自动加载.env.testing,优先于主.env文件。

2. 修改测试基类的URL配置(可选)

如果不想单独维护.env.testing,可以在tests/TestCase.php中重写createApplication方法,强制设置测试环境的URL:

use Illuminate\Contracts\Console\Kernel;

protected function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';

    $app->make(Kernel::class)->bootstrap();

    // 强制设置测试环境的基础URL
    config(['app.url' => 'http://localhost']);

    return $app;
}

3. 无需调整Lando核心配置

问题根源是Laravel测试套件对APP_URL的解析逻辑,Lando的服务配置无需修改,保持默认即可。


内容的提问来源于stack exchange,提问作者Chris Rockwell

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 22:55:16