Laravel 4 - 未找到控制器

时间:2014-02-18 13:46:54

标签: mysql laravel laravel-4 laravel-routing

我正在使用laravel的应用程序,它将文件保存在数据库和目标文件夹中。我试图从数据库中删除一条记录,但我得到的是一个错误,表示找不到控制器方法。

错误:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
Controller method not found.

throw new NotFoundHttpException("Controller method not found.");

视图alluploads.blade.php有一个表单,它应该将id传递给uploads / destroy函数。

{{ Form::open(array('method' => 'DELETE', 'url' => array('uploads/destroy', $upload->id))) }}
     {{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
{{ Form::close() }}

我的控制器具有删除行的功能。

public function getDestroy($id)
{
     $this->upload->find($id)->delete();
     return Redirect::to('uploads/alluploads')->with('message', 'Thanks, delete was successful!');
}

路线有以下内容来访问上传控制器

下的功能
Route::controller('uploads', 'UploadsController');

2 个答案:

答案 0 :(得分:3)

您正在使用HTTP方法DELETE,因此您的控制器方法应为:

public function deleteDestroy($id)
{
     $this->upload->find($id)->delete();
     return Redirect::to('uploads/alluploads')->with('message', 'Thanks, delete was successful!');
}

看一下你的路线:

php artisan routes

你必须至少看到这样的事情:

+--------+---------------------------------------------------------------+----------------------+-------------------------------------------------------+--------------------------------+---------------+
| Domain | URI                                                           | Name                 | Action                                                | Before Filters                 | After Filters |
+--------+---------------------------------------------------------------+----------------------+-------------------------------------------------------+--------------------------------+---------------+
|        | DELETE uploads/destroy/{one?}/{two?}/{three?}/{four?}/{five?} |                      | UploadsController@deleteDestroy                       |                                |               |
|        | GET uploads/{_missing}                                        |                      | UploadsController@missingMethod                       |                                |               |
+--------+---------------------------------------------------------------+----------------------+-------------------------------------------------------+--------------------------------+---------------+

要使用您的表单,您的表单开放行必须是:

Form::open(array('method' => 'GET', 'url' => array('uploads/destroy', $upload->id)))

但我认为现在的方式更好,只需重命名你的方法。

答案 1 :(得分:1)

首先尝试使用上传/而不是上传/销毁。删除的路由名称是resource.destroy,但是url是使用删除动词调用的resource / {id}

如果$ uploads是上传索引

,以下是打开表单创建删除按钮的方法
@foreach($uploads as $upload)
    {{ Form::model($upload, array('url' => 'uploads/'. $upload->id, 'method' => 'delete')) }}
    {{ Form::submit('delete') }}
    {{ Form::close() }}
@endofreach