php laravel NotFoundHttpException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23430205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel NotFoundHttpException
提问by Antonio Carlos Ribeiro
I am new at laravel. I am trying to make a link to another page. I have the page index and want to go to desc which display information about a vehicle selected in index page. The problem is that it shows the error:
我是 Laravel 的新手。我正在尝试链接到另一个页面。我有页面索引,想转到 desc,其中显示有关在索引页面中选择的车辆的信息。问题是它显示了错误:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
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'));
回答by Antonio Carlos Ribeiro
The 'NotFoundHttpException' means Laravel wasn't able to find a route to for the request.
'NotFoundHttpException' 意味着 Laravel 无法找到请求的路由。
Your desc
route is a POST route, only, and a link_to_action
will create a GET request, so you may need to change add a GET route too:
您的desc
路由只是一个 POST 路由,并且 alink_to_action
将创建一个 GET 请求,因此您可能还需要更改添加一个 GET 路由:
Route::post('desc', array('uses' => 'CarController@show'));
Route::get('desc', array('uses' => 'CarController@show'));
There's also a any
, which does GET, POST, PUT, DELETE:
还有一个any
, 可以执行 GET、POST、PUT、DELETE:
Route::any('desc', array('uses' => 'CarController@show'));
If you need to get and id
from your route, you will have to add it as a parameter:
如果您需要id
从您的路线获取和发送,则必须将其添加为参数:
Route::post('car/{id}', array('uses' => 'CarController@show'));
And you will have to access your page as:
您必须以以下方式访问您的页面:
http://myappt.al/public/car/22
But if you want to access it as:
但是,如果您想以以下方式访问它:
http://myappt.al/public/22
You'll need to do:
你需要做:
Route::post('{id}', array('uses' => 'CarController@show'));
But this one is dangerous, because it will may grab all the routes, so you MUST to set it as your very last route.
但是这个很危险,因为它可能会抓取所有的路线,所以你必须把它设置为你的最后一条路线。
And your controller must accept that parameter:
并且您的控制器必须接受该参数:
class CarController extends Controller {
public function show($id)
{
dd("I received an ID of $id");
}
}
EDIT:
编辑:
Since you most of your routes made manually, you can also go with the index this way:
由于您的大部分路线都是手动制作的,因此您也可以通过以下方式使用索引:
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'));