我是 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
路由.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'));
最佳答案
'NotFoundHttpException' 意味着 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'));
如果您需要从您的 route 获取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'));
但是这个很危险,因为它可能会抢走所有的路由,所以你必须将它设置为你的最后一条路由。
并且您的 Controller 必须接受该参数:
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'));
关于php - laravel NotFoundHttpException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23430205/