我试图使用Laravel 5.1的表单请求验证来授权请求是否来自所有者.当用户试图通过show.blade.php更新表clinics的一部分时,使用验证.

到目前为止,我的设置:

routes.php:

Route::post('clinic/{id}', 
    array('as' => 'postUpdateAddress', 'uses' => 'ClinicController@postUpdateAddress'));

ClinicController.php:

public function postUpdateAddress($id, 
        \App\Http\Requests\UpdateClinicAddressFormRequest $request)
    {
        $clinic             = Clinic::find($id);
        $clinic->save();

        return Redirect::route('clinic.index');
    }

UpdateClinicAddressFormRequest.php:

public function authorize()

    {
        $clinicId = $this->route('postUpdateAddress');

        return Clinic::where('id', $clinicId)
        ->where('user_id', Auth::id())
        ->exists();
    }

Show.blade.php

{!! Form::open(array('route' => array('postUpdateAddress', $clinic->id), 'role'=>'form')) !!}

{!! Form::close() !!}

如果在authorize函数中输入dd($clinicId),则返回null,因此我认为这就是问题所在!

如果您在提交时说"禁止",我们将不胜感激.

推荐答案

您得到的是Forbidden Error,因为表单请求的authorize()方法返回false:

问题是:$clinicId = $this->route('postUpdateAddress');

要访问表单请求中的路由参数值,可以执行以下操作:

$clinicId = \Route::input('id'); //to get the value of {id}

所以authorize()应该是这样的:

public function authorize()
{
    $clinicId = \Route::input('id'); //or $this->route('id');

    return Clinic::where('id', $clinicId)
    ->where('user_id', Auth::id())
    ->exists();
}

Laravel相关问答推荐

如何通过 Eloquent 获取单个集合中每位教师的学生人数

如何在自定义表单请求中获取错误验证数据

如何从@foreach 循环中捕获所有数据并将其传递给 值? - Laravel

如何在数据来自Rest API的flutter中创建下拉类别名称列表?

语法错误或访问冲突:1115 未知字符集:utf8mb4

Laravel 6 Passport 在错误的凭证上返回 400 Bad request

Lumen 中的自定义 404 页面

Laravel 4:验证前修剪输入的最佳实践

Laravel 框架类在 PHPUnit 数据提供程序中不可用

中间件中的 Laravel 依赖注入

如何在 Laravel 5.2 中手动发送密码重置请求?

WhereNotExists Laravel Eloquent

Laravel 在特定文件夹中创建迁移文件

Laravel:命令未找到

扩展模型 == 扩展 Eloquent?

将自定义消息(或任何其他数据)传递给 Laravel 404.blade.php

Laravel X-CSRF-Token 与 POSTMAN 不匹配

如何以及在哪里可以使用 laravel 存储图像?

如何让 Laravel 将 View 的Content-Type标头返回为application/javascript?

如何判断 Laravel 集合是否为空?