我在服务器上部署了Laravel
中的应用程序。当我尝试在Postman
中调用POST
时:
http://api.mydomain.com/api/auth/signin
我得到错误404 not found
。
在文件RouteServiceProvider
中,我为web注释了部分:
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
/*
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
*/
});
}
当我对web和从邮递员发送GET
的未注释部分,但没有任何后缀,只有域:api.mydomain.com/它工作。现在我评论了web,只有API部分,任何对API方法的请求都不起作用。我的控制器:
class JwtAuthController extends Controller
{
//
public function __construct() {
$this->middleware('auth:api', ['except' => ['login', 'register']]);
}
/**
* Get a JWT via given credentials.
*/
public function login(Request $request){
...
}
在api.php
文件:
Route::group([
'middleware' => 'api',
'prefix' => 'auth'
], function ($router) {
Route::post('/signin', [JwtAuthController::class, 'login']);
}
Result forphp artisan route:list
:
| AppHttpMiddlewareAuthenticate:api |
|
| POST
| api/auth/signin
| generated::1YOyMAVvTTnBZtym
| AppHttpControllersJwtAuthController@login
| api
在我的本地机器上,它可以工作,但是当我将它部署到服务器上时,它不能工作。
没有找到,因为您没有添加子域api
,要修复它
你可以从url中删除api.
,就像http://exmaple.com/api/auth/signin
或
您可以将子域添加到RouteServiceProvider
这样的
Route::middleware('api')
->prefix('api')
->domain('api.exmaple.com') // <-
->namespace($this->namespace)
->group(base_path('routes/api.php'));