Laravel 5表单请求验证返回禁止错误

问题描述 投票:13回答:2

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

我到目前为止的设置:

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() !!}

如果我在授权函数中使用dd($clinicId),它会返回null,所以我认为这就是问题所在!

任何帮助,为什么提交它说'禁止'将非常感激。

php validation laravel laravel-4 laravel-5
2个回答
34
投票

您收到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();
}

3
投票

我将此所有者确认添加到Request和work中的authorize()方法

public function authorize()
{
    return \Auth::check();
}
© www.soinside.com 2019 - 2024. All rights reserved.