首页 > 解决方案 > Laravel 路由与 Application 在子目录中的位置不匹配

问题描述

我在子目录中有我的 Laravel 应用程序:

https://www.example.com/subdirectory/api

这意味着对 API 的请求是这样完成的:

// GET https://www.example.com/subdirectory/api/user/1

// Meanwhile in api.php
Route::get('/user/{id}', 'controllerName@methodName');

并按预期返回。

但是,当尝试使用$this->get()测试用例上的方法设置这些端点的功能测试时,我的路由不匹配。这似乎正在发生,因为整个子目录结构都包含在最终路由中:

// In Test
$response = $this->get('/user/2');

// In api.php this no longer matches
Route::get('/user/{id}', 'controllerName@methodName'); 

// But this does
Route::get('/subdirectory/api/user/{id}', 'controllerName@methodName'); 

json基本上,在使用任何/ get/post等方法从测试中查询之前,路由似乎工作得非常好。请求中的文字字符串 URL 完全没问题(没有重复的子目录等)。

为什么我的路由匹配真实查询而不是这些测试查询?

标签: phplaravelhttptestingphpunit

解决方案


推荐阅读