路线未定义

Route not defined

这是我的路线:

$app->group(['prefix' => 'book/'], function ($app) {
    $app->get('/','BooksController@index'); //get all the routes    
    $app->post('/','BooksController@store'); //store single route
    $app->get('/{id}/', 'BooksController@show'); //get single route
    $app->put('/{id}/','BooksController@update'); //update single route
    $app->delete('/{id}/','BooksController@destroy'); //delete single route
});

当我尝试生成 URL 时,系统 return 未定义路线 [book]。

@foreach ($books as $book)
    <li>
        <a href="{{ route('book', ['id' => $book->id]) }}">
            {{ $book->name}}
        </a>
    </li>
@endforeach

我想念什么?

您没有为路线命名

$app->group(['prefix' => 'book/'], function ($app) {

   $app->get('/{id}/', [ 
      'as' => 'book', 
      'uses' => 'BooksController@show'
   ]);

});

然后你可以这样做

@foreach ($books as $book)
<li>
    <a href="{{ route('book', ['id' => $book->id]) }}">
        {{ $book->name}}
    </a>
</li>
@endforeach

这样试试看,可读性更好:

# Book routes
Route::group(['prefix' => 'books'], function ()
{
    Route::get('/', ['as' => 'index', 'uses' => 'BooksController@index']);
    Route::post('store', ['as' => 'store', 'uses' => 'BooksController@store']);
    Route::get('show/{id}', ['as' => 'show', 'uses' => 'BooksController@show']);
    Route::post('update/{id}', ['as' => 'update', 'uses' => 'BooksController@update']);
    Route::delete('destroy/{id}', ['as' => 'destroy', 'uses' => 'BooksController@destroy']);
});

我也认为你不需要put方法,你可以在你的表单中使用update + method spoofing。有关方法欺骗的更多信息,请查看 docs