我是laravel的新人。我正在尝试建立到另一个页面的链接。我有页面索引,并且想要显示关于在索引页面中选择的车辆的信息的desc。 问题是它显示错误:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
index.blade.php
@foreach ($cars as $car)
<tr>
<td>
{{link_to_action('CarController@show', $car->Description, $car->id)}}</td>
{{ Form::open(array('action' => 'CarController@show', $car->id)) }}
{{ Form::close() }}
<td>{{ $car->License }}</td>
<td>{{ $car->Milage }}</td>
<td>{{ $car->Make }}</td>
<td>{{ $car->status }}</td>
</tr>
@endforeach
routes.php文件
Route::resource('/', 'CarController');
Route::resource('create', 'DataController');
Route::post('desc', array('uses' => 'CarController@show'));
Route::post('create', array('uses' => 'CarController@create', 'uses' => 'DataController@index'));
Route::post('update', array('uses' => 'CarController@update'));
Route::post('store', array('store' => 'CarController@store'));
答案 0 :(得分:13)
&#39; NotFoundHttpException&#39;意味着Laravel无法找到满足请求的路线。
您的desc
路由只是一个POST路由,而link_to_action
会创建一个GET请求,因此您可能还需要更改添加GET路由:
Route::post('desc', array('uses' => 'CarController@show'));
Route::get('desc', array('uses' => 'CarController@show'));
还有any
,它可以执行GET,POST,PUT,DELETE:
Route::any('desc', array('uses' => 'CarController@show'));
如果您需要从路线中获取id
,则必须将其添加为参数:
Route::post('car/{id}', array('uses' => 'CarController@show'));
您必须以下列方式访问您的网页:
http://myappt.al/public/car/22
但是如果你想以下列方式访问它:
http://myappt.al/public/22
您需要这样做:
Route::post('{id}', array('uses' => 'CarController@show'));
但这个很危险,因为它可能会抓住所有路线,所以你必须将它设置为你最后的路线。
您的控制器必须接受该参数:
class CarController extends Controller {
public function show($id)
{
dd("I received an ID of $id");
}
}
编辑:
由于你手动制作了大多数路线,你也可以这样使用索引:
Route::resource('create', 'DataController');
Route::get('/', 'CarController@index');
Route::post('create', array('uses' => 'CarController@create','uses' => 'DataController@index'));
Route::post('update', array('uses' => 'CarController@update'));
Route::post('store', array('store' => 'CarController@store'));
Route::get('{id}', array('uses' => 'CarController@show'));
Route::post('{id}', array('uses' => 'CarController@show'));