Laravel Api 路由加载失败。404 未找到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32483909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:19:29  来源:igfitidea点击:

Laravel Api routes fail to load. 404 not found

phpangularjslaravel

提问by JCairoli

For reference I have been working with this tutorial https://scotch.io/tutorials/build-a-time-tracker-with-laravel-5-and-angularjs-part-2.

作为参考,我一直在使用本教程https://scotch.io/tutorials/build-a-time-tracker-with-laravel-5-and-angularjs-part-2

I wanted to become more familiar with laravel 5 since I had previously only used 4 and found the above tutorial which also mixed in a little angular js. I followed part one and two of the tutorials to the letter and set up a database using mysql and phpmyadmin like directed.

我想更熟悉 laravel 5,因为我之前只使用过 4,并且发现上面的教程也混入了一点 angular js。我按照教程的第一部分和第二部分进行操作,并按照指示使用 mysql 和 phpmyadmin 设置了一个数据库。

I get to a section about halfway through which sets up a group route with the prefix api to pull seeded data from the database and display it in the view.

我进入了大约一半的部分,该部分设置了一个带有前缀 api 的组路由,以从数据库中提取种子数据并将其显示在视图中。

// app/Http/routes.php

 ...

// A route group allows us to have a prefix, in this case api
Route::group(array('prefix' => 'api'), function()
{
    Route::resource('time', 'TimeEntriesController');
    Route::resource('users', 'UsersController');
});

After this point I go to the page and the area that was previously rendered with data from a file instead of a database is now blank. If I inspect the element I get "failed to load resource the server responded with a status of 404 (not found)" and it displays my path time-tracker-2/public/api/time.

在此之后,我转到页面,之前使用文件而不是数据库中的数据呈现的区域现在是空白的。如果我检查元素,我会得到“加载资源失败,服务器响应状态为 404(未找到)”,它显示我的路径 time-tracker-2/public/api/time。

The routes work with these two controllers to populate the page with userdata from my database

路由与这两个控制器一起使用我的数据库中的用户数据填充页面

// app/Http/Controllers/TimeEntriesController.php

...

use App\Http\Requests;
use App\Http\Controllers\Controller;    
use App\TimeEntry;

use Illuminate\Support\Facades\Request;

class TimeEntriesController extends Controller {

    // Gets time entries and eager loads their associated users
    public function index()
    {
        $time = TimeEntry::with('user')->get();

        return $time;
    }


// app/Http/Controllers/UsersController.php

...

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;

use Illuminate\Http\Request;

class UsersController extends Controller {

    // Gets all users in the users table and returns them
    public function index()
    {
        $users = User::all();

        return $users;
    }

Again I haven't worked much with Laravel and this is my first time messing with angular so I don't know if I am missing something super obvious or what the deal is. I have checked over all my code and compared it to the sample code and they are identical other than my database information. I have also scrapped the project and started from scratch and still get the same error when I get to this point.

我再次与 Laravel 合作不多,这是我第一次搞乱 angular,所以我不知道我是否遗漏了一些非常明显的东西或者交易是什么。我检查了我的所有代码并将其与示例代码进行了比较,除了我的数据库信息之外,它们是相同的。我也取消了该项目并从头开始,但当我到达这一点时仍然遇到相同的错误。

Any sort of direction to look would be greatly appreciated because this error is driving me nuts.

任何方向都将不胜感激,因为这个错误让我发疯。

采纳答案by cre8

Remember to point on the public folder, not on the root folder. That's why your URL is time-tracker-2/public/api/timeand not time-tracker-2/api/time. This should fix your 404 error.

请记住指向公共文件夹,而不是根文件夹。这就是为什么您的 URL 是time-tracker-2/public/api/time而不是time-tracker-2/api/time. 这应该可以修复您的 404 错误。