我想将验证器与控制器分开,但API始终在Postman中以200 OK
响应。
要求验证:
class PostRequest extends FormRequest
{
use Types;
public function authorize()
{
return true;
}
public function rules()
{
// $auth = $this->request->user();
return [
'name' => 'required|string|max:255',
'country_id' => 'required|exists:countries,id',
'city' => 'required|max:255',
'phone_no' => 'required|regex:/[0-9]{10,13}/',
'occupation' => 'required|max:255'
];
}
}
SenderController:
public function store(PostRequest $request)
{
$auth = $request->user();
$sender = Sender::create([
'name' => $request->name,
'country_id' => $request->country_id,
'city' => $request->city,
'phone_no' => $request->phone_no,
'occupation' => $request->occupation
]);
return new Resource($sender);
}
当我发送不带name
的请求时,它将返回状态为200的响应。当我忘记输入名称时,我想在响应中显示$validator->errors()
。我怎样才能做到这一点?
路线和通话:
Route::post('sender', 'V1\SenderController@store');
POST: localhost:8000/api/v1/sender
参考方案
您应该确保使用Accept: application/json
标头发送请求。
否则,Laravel将无法检测到它是一个API请求,并且不会生成带有错误的422响应。
PHP:对数组排序 - php请如何排序以下数组Array ( 'ben' => 1.0, 'ken' => 2.0, 'sam' => 1.5 ) 至Array ( 'ken' => 2.0, 'sam' => 1.5, 'ben' =&…
PHP strtotime困境 - php有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00…
PHP-全局变量的性能和内存问题 - php假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…
php-casperjs获取内部文本 - php我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…
PHP PDO组按列名称查询结果 - php以下PDO查询返回以下结果:$db = new PDO('....'); $sth = $db->prepare('SELECT ...'); 结果如下: name curso ABC stack CDE stack FGH stack IJK stack LMN overflow OPQ overflow RS…