调用控制器上的索引方法,但未在控制器中使用

Index method on controller is called, but not used in controller

我正在做一个 Laravel 项目,我 运行 遇到了一个问题,我无法在不抛出 404 错误的情况下让我的索引页面工作。

我在一个业余创作网站上工作,该网站有一个故事数据库 - 用户可以上传文本或文件等。我有一个索引页面,它以很长的形式显示所有故事 table .这是页面和调用它的控制器方法:

控制器(仅显示相关方法):

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
    $stories = Story::latest()->get();
    return view('stories.index', compact('stories'));
}

index.blade.php:

@extends('base')

@section('main')
    <div class="row">
        <div class="col-sm-12">
            <h1 class="display-3">Stories</h1>
            <table class="table table-striped">
                <thead>
                <tr>
                    <td>Story ID</td>
                    <td>Title</td>
                    <td>Author ID</td>
                    <td>Genre</td>
                    <td colspan = 2>Options</td>
                </tr>
                </thead>
                <tbody>
                @foreach($stories as $story)
                    <tr>
                        <td>{{$story->id}}</td>
                        <td>{{$story->title}}</td>
                        <td>{{$story->authorid}}</td>
                        <td>{{$story->genre1}}</td>
                        <td>
                            <a href="{{ route('stories.show', $story->id)}}" class="btn btn-primary">View</a>
                        </td>
                        <td>
                            <a href="{{ route('stories.edit', $story->id)}}" class="btn btn-primary">Edit</a>
                        </td>
                        <td>
                            <form action="{{ route('stories.destroy', $story->id)}}" method="post">
                                @csrf
                                @method('DELETE')
                                <button class="btn btn-danger" type="submit">Delete</button>
                            </form>
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>
@endsection

当我尝试访问此页面时,我不断收到 404 错误。我知道我以前能够访问它,因为它引发了与数据库相关的其他错误,但是当这些错误被修复后,我得到了 404。我不知道我在哪里可能会出错。它还在控制器方法定义中说该元素未被使用——我不知道为什么会这样说,因为它明确地从路由文件中引用:

    Route::get('/', function () {
    return view('welcome');
});

Route::get('/soon', function () {
    return view('soon');
});

Route::resource('stories', 'StoryController');
Route::post('/stories', 'StoryController@store');
Route::get('stories/create', 'StoryController@create');
Route::get('stories/index', 'StoryController@index');

如有任何帮助,我们将不胜感激!

当您定义 resource 路由时,您不需要定义任何其他 CRUD 路由,如前所述 here

Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code

...

This single route declaration creates multiple routes to handle a variety of actions on the resource. The generated controller will already have methods stubbed for each of these actions, including notes informing you of the HTTP verbs and URIs they handle.

我的猜测是这两条路线可能相互碰撞

Route::resource('stories', 'StoryController'); Route::get('stories/index', 'StoryController@index');

因此您不需要以下路线:

Route::post('/stories', 'StoryController@store'); Route::get('stories/create', 'StoryController@create'); Route::get('stories/index', 'StoryController@index');

此外,请确保您重定向到目录中的正确视图。如果路径正确