【问题标题】:Laravel API route not found 404Laravel API 路由未找到 404
【发布时间】:2020-10-26 05:29:58
【问题描述】:

我已经设置api.php如下

<?php

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

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

/*
Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});
*/


Route::get('trip', 'TripController@getTrip');

虽然我先尝试了中间件,但我注释掉了它。只是为了排除它不会以某种方式影响。

在我的 TripController 中,我有:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Trips;

class TripController extends Controller
{
    public function getTrip() {
        $jeekim = "JEEKIM";
        dd($jeekim);

        return response()->json(Trips::get(), 200);
    }
}

我已经建立了我的数据库,并在其中设置了一些数据进行测试。据我所知,去浏览器 xxx.xxx.xxx.xxx/api/trip 应该打开这个? 但相反,我得到 404: 在此服务器上找不到请求的 URL。 我在控制器和 dd 中添加了变量只是为了查看我是否达到了控制器功能但没有。

我确定我启用了 a2enmod 重写。 我使用海洋滴云服务器,而不是在本地主机上。我在 web.php 中有一条测试路线,它可以工作,我可以返回一个视图。但是这个api路由我不能上班。

【问题讨论】:

  • 运行命令php artisan route:list. 它将显示您应用程序中可用路由的列表。
  • 感谢您的回复! Vipin 的回答让我找到了解决方案。我的 apache2.conf 有问题,更准确地说是在 sites-enabled/000-default.conf 我让它工作

标签: php laravel


【解决方案1】:

在您的 apache2.conf 中确保您已 AllowOverride All 而非 AllowOverride none。

示例-

<Directory /var/www/gloops/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

【讨论】:

  • 谢谢!!!!在 sties-enabled/000-default.conf 中有一些问题我让它工作了:)
  • 伟大的开始@Vipin
【解决方案2】:

.htaccess 文件从公用文件夹移动到应用程序根目录,并将应用程序根目录中的 server.php 文件重命名为 index.php

【讨论】:

  • 这个问题已经有一个接受的答案。你能解释一下为什么你的方法更好吗?
  • @LajosArpad 事实上,这不是一个正确的答案,因为这个答案正在改变 Laravel 的工作方式,当修复用户问题与移动或编辑与 Laravel 本身相关的任何内容时......跨度>
猜你喜欢
  • 2022-01-14
  • 2014-06-25
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 2021-11-06
  • 2018-12-27
  • 1970-01-01
相关资源
最近更新 更多