我正尝试使用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() !!}
如果我在授权函数中使用
dd($clinicId)
,它会返回null
,所以我认为问题就出在那里!
请问有任何帮助可以解决“禁止访问”的问题吗?非常感谢。