2015-06-16 20 views
11

Tôi đang cố gắng sử dụng xác thực yêu cầu biểu mẫu của Laravel 5.1, để cho phép nếu yêu cầu từ chủ sở hữu. Xác thực được sử dụng khi người dùng đang cố gắng cập nhật một phần của bảng clinics thông qua show.blade.php.Xác nhận yêu cầu biểu mẫu của Laravel 5 trả lại lỗi bị cấm

tôi thành lập cho đến nay:

routes.php:

Route::post('clinic/{id}', 
    array('as' => 'postUpdateAddress', 'uses' => '[email protected]')); 

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

Nếu tôi dd($clinicId) bên trong hàm ủy quyền, nó sẽ trả null, vì vậy tôi nghĩ rằng đó là nơi mà vấn đề nằm!

Bất kỳ trợ giúp nào về việc gửi câu hỏi 'bị cấm' sẽ được đánh giá cao.

Trả lời

27

Bạn đang nhận được Forbidden Lỗiauthorize() phương pháp đề nghị hình thức đang trở lại sai:

Vấn đề là thế này: $clinicId = $this->route('postUpdateAddress');

Để truy cập một giá trị tham số tuyến đường trong Form yêu cầu bạn có thể làm điều này :

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

so authorize() nên xem xét như thế này:

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

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

Cảm ơn bạn rất nhiều vì sự giúp đỡ của bạn! Một cái gì đó rất đơn giản. – Ben

+0

Chào mừng bạn. Happy code – Digitlimit

2

tôi thêm xác nhận chủ sở hữu này cho phép() phương pháp trong Yêu cầu và làm việc

public function authorize() 
{ 
    return \Auth::check(); 
} 
Các vấn đề liên quan