namespace App\Http\Requests\Users;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
class UpdateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
/**
* By default it returns false, change it to
* something like this if u are checking authentication
*/
return Auth::check(); // <-------
/**
* You could also use something more granular, like
* a policy rule or an admin validation like this:
* return auth()->user()->isAdmin();
*
* Or just return true if you handle the authorisation
* anywhere else:
* return true;
*/
}
public function rules()
{
// your validations...
}
}
当我在函数public function authorize()中的php artisan make:request SellRequest中没有return true时,我遇到了这个问题
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SellRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'city'=>'required',
'address'=>'required',
'type'=>'required',
'land'=>'required',
'area'=>'required'
];
}
}
<?php
namespace App\Modules\UserManagement\Request;
use Illuminate\Foundation\Http\FormRequest;
use Response;
class UserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
public function rules()
{
$rules = [
'full_name' => 'required',
'email' => 'required|email',
'password' => 'required',
're_enter_password' => 'required'
];
return $rules;
}
}
9条答案
按热度按时间ao218c7q1#
不久前,我在开始使用Form Request类进行数据验证时遇到了类似的问题。
如果您使用表单请求来验证数据,那么首先,检查您是否正确设置了允许它通过的授权规则。这由
authorize()
方法处理,该方法必须返回boolean
,默认情况下设置为false
:kr98yfug2#
确保在“authorize”方法上返回**true
1dkrff033#
对于Laravel 8,进入文件夹app-〉http-〉requests,选择类文件(在我的例子中是StoreStudentRequest.php),并在函数authorize中设置返回值为true;
kx7yvsdv4#
当我在函数
public function authorize()
中的php artisan make:request SellRequest
中没有return true
时,我遇到了这个问题g52tjvyc5#
1szpjjfi6#
在我的例子中,我没有在
Gate::define(...)
中执行正确的检查所以也许要仔细检查一下这个函数的逻辑
tsm1rwdh7#
需要授权函数返回true
然后添加auth facade或使用Auth;
o2rvlv0m8#
如果您已经配置了authorize(),但仍然遇到同样的问题,您可以检查您的route/api.php您可能会遇到为2 Controller声明相同路径的错误。
trnvg8h39#
在你的控制器中添加这一行
代替
并将函数中的参数更改为