【问题标题】:Laravel 4 - Controller not foundLaravel 4 - 找不到控制器
【发布时间】:2014-03-18 07:47:00
【问题描述】:

我正在使用 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');

【问题讨论】:

    标签: mysql laravel laravel-4 laravel-routing


    【解决方案1】:

    您正在使用 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)))
    

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

    【讨论】:

    • 感谢安东尼奥的回答,它有效。我正在终端中使用 php arisan 路由,它非常有帮助。我不知道 deleteDestroy 会起作用,虽然我只是发布和获取工作。再次感谢!
    • 这很有帮助,laravel.com/docs/controllers。如果我将我的表单打开行更改为使用 get 方法而不是 delete,它仍然会给我一个错误。我所做的只是按照您的建议更改方法的名称。
    【解决方案2】:

    首先尝试仅使用 uploads/ 而不是 uploads/destroy 的功能。删除的路由名称是resource.destroy,但url是resource/{id},用delete动词调用

    如果 $uploads 是上传的索引,我如何打开表单以创建删除按钮

    @foreach($uploads as $upload)
        {{ Form::model($upload, array('url' => 'uploads/'. $upload->id, 'method' => 'delete')) }}
        {{ Form::submit('delete') }}
        {{ Form::close() }}
    @endofreach
    

    【讨论】:

      猜你喜欢
      • 2016-04-04
      • 2014-03-09
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 2014-09-25
      • 2013-03-13
      相关资源
      最近更新 更多