路线未定义

时间:2017-06-17 18:29:45

标签: php laravel lumen

这是我的路线:

$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时,系统会返回未定义的Route [book]。

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

我想念的是什么?

2 个答案:

答案 0 :(得分:2)

您没有为路线命名

$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

答案 1 :(得分:1)

以这种方式尝试,因此它更具可读性:

# 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。有关方法欺骗的更多信息,请查看https://sqlperformance.com/2012/07/t-sql-queries/split-strings