php - CRUD Laravel 4如何链接销毁?

标签 php laravel laravel-4

我将使用 HTML 链接破坏我的用户,但它似乎没有生成正确的链接,我做错了什么?

public function destroy($id)
{
    //Slet brugeren
    $e = new User($id);
    $e->destroy();

    //Log også brugeren ud
    Auth::logout();

    //redrect til forsiden
    Redirect::to("users/create");
}

在我看来我称之为 {{URL::action('UserController@destroy', array($user->id))}}

最佳答案

2017 年 8 月 21 日更新 Laravel 5.x

这个问题是关于 Laravel 4 的,但我把它包括在内,以防寻找 Laravel 5.x 答案的人在这里结束。表单助手(和其他一些)aren't available as of 5.x .如果您正在执行 GET 或 POST 之外的操作,您仍然需要在表单上指定一个方法。这是目前的实现方式:

<form action="/foo/bar" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <!-- other inputs... -->
</form>

您也可以使用 {{ method_field('PUT') }} 而不是写出隐藏的 _method 输入。

https://laravel.com/docs/5.4/routing#form-method-spoofing

Laravel 4 的原始答案

我认为当您单击链接时,它可能正在向该端点发送 GET 请求。 Laravel 中的 CRUD 根据 REST 工作。这意味着它期待的是 DELETE 请求而不是 GET。

这是 tutorial 中的一种可能性鲍里斯·斯特拉希亚(Boris Strahija)。

    {{ Form::open(array('route' => array('admin.pages.destroy', $page->id), 'method' => 'delete')) }}
        <button type="submit" class="btn btn-danger btn-mini">Delete</button>
    {{ Form::close() }}

这样,您可以使用 DELETE 方法以表单形式发送请求。这篇文章解释了为什么传统链接不起作用:

You may notice that the delete button is inside a form. The reason for this is that the destroy() method from our controller needs a DELETE request, and this can be done in this way. If the button was a simple link, the request would be sent via the GET method, and we wouldn’t call the destroy() method.

关于php - CRUD Laravel 4如何链接销毁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19643483/

相关文章:

php - 创建复杂的 hasManyThrough Laravel 4 查询

php - Facebook 请求权限示例?

php - 使用 Symfony 2 在 Twig 中本地化日期

php - 如何使用 composer.json 在 Laravel 5 中自动加载新文件夹?

laravel - 无法实例化抽象类 Illuminate\Database\Eloquent\Model - Laravel 4

orm - Laravel Eloquent 访问原文

php - Laravel 5.2 - 如何将变量(数组和整数)从 View 传递到 Controller ?

php - Laravel 属于/有很多

php - 如何获取 laravel 中所有服务器 session 的列表?

php - 使用 laravel 在字段模式迁移中定义属性 zerofill 和大小