【问题标题】:Laravel 5 form request validation returning forbidden errorLaravel 5表单请求验证返回禁止错误
【发布时间】:2015-09-01 21:25:43
【问题描述】:

我正在尝试使用 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,所以我认为这就是问题所在!

任何帮助为什么在提交时说“禁止”将不胜感激。

【问题讨论】:

    标签: php validation laravel laravel-4 laravel-5


    【解决方案1】:

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

    【讨论】:

    • 非常感谢您的帮助!这么简单的事情。
    • 欢迎。快乐编码
    【解决方案2】:

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

    public function authorize()
    {
        return \Auth::check();
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 2020-01-17
      • 1970-01-01
      • 2017-03-14
      • 2016-05-20
      • 2016-01-20
      • 1970-01-01
      • 2016-08-30
      • 2017-11-07
      相关资源
      最近更新 更多