使用 Phalcon PHP 作为 json return 验证消息的最佳方法是什么
What is the best way to return validation messages with Phalcon PHP as json
我正在创建 REST API,我想 return 来自验证的消息。
这里是用户注册的例子:
class Users extends Phalcon\Mvc\Model {
public function validation() {
$validator = new Validation();
$validator->add('email', new PresenceOf([
'message' => 'The email is required'
]));
$validator->add('email', new Email([
'message' => 'Invalid email given'
]));
$validator->add('email', new Uniqueness([
'message' => 'Sorry, The email was registered by another user'
]));
return $this->validate($validator);
}
这是我当前的代码:
class UserController extends Phalcon\Mvc\Controller {
public function register() {
$data = $this->request->getJsonRawBody();
$user = new Users();
$user->setEmail($data->email)->setUsername($data->username)->setPassword($data->password);
if ($user->save() === false) {
$data = [];
$messages = $user->getMessages();
foreach ($messages as $message) {
$data[] = array(
'field' => $message->getField(),
'type' => $message->getType(),
'message' => $message->getMessage()
);
}
return $this->response->setStatusCode(409, 'Conflict')->setJsonContent(array('error' => $data));
}
return $this->response->setStatusCode(201, 'Created')->setJsonContent(array('message' => "User id:" . $user->getId() . " created"));
}
当我用类似的东西更改此 for 循环时,我得到空响应:
return $this->response->setStatusCode(409, 'Conflict')->setJsonContent(array('error' => $user->getMessages()));
问题是有没有可能使这段代码更短而不通过消息循环,但只是简单的 return json 对象。
您可以通过扩展默认方法进行很少的修改就可以做到这一点。这是您的代码的工作示例:
// BaseModel class which overwrites default Phalcon methods if needed.
class BaseModel extends \Phalcon\Mvc\Model
{
public function getMessagesNormalized()
{
$messages = parent::getMessages();
$data = [];
foreach ($messages as $message) {
$data[] = array(
'field' => $message->getField(),
'type' => $message->getType(),
'message' => $message->getMessage()
);
}
return $data;
}
}
// Note that your models from now on extend BaseModel which extends \Phalcon\Mvc\Model
class Users extends BaseModel {
// ...
}
// Your controller
class UserController extends Phalcon\Mvc\Controller {
public function register() {
$data = $this->request->getJsonRawBody();
$user = new Users();
$user->setEmail($data->email)->setUsername($data->username)->setPassword($data->password);
if ($user->save() === false) {
$data = [];
// Get the messages in RESTsuitable format
$messages = $user->getMessagesNormalized();
return $this->response->setStatusCode(409, 'Conflict')->setJsonContent(array('error' => $data));
}
return $this->response->setStatusCode(201, 'Created')->setJsonContent(array('message' => "User id:" . $user->getId() . " created"));
}
我正在创建 REST API,我想 return 来自验证的消息。
这里是用户注册的例子:
class Users extends Phalcon\Mvc\Model {
public function validation() {
$validator = new Validation();
$validator->add('email', new PresenceOf([
'message' => 'The email is required'
]));
$validator->add('email', new Email([
'message' => 'Invalid email given'
]));
$validator->add('email', new Uniqueness([
'message' => 'Sorry, The email was registered by another user'
]));
return $this->validate($validator);
}
这是我当前的代码:
class UserController extends Phalcon\Mvc\Controller {
public function register() {
$data = $this->request->getJsonRawBody();
$user = new Users();
$user->setEmail($data->email)->setUsername($data->username)->setPassword($data->password);
if ($user->save() === false) {
$data = [];
$messages = $user->getMessages();
foreach ($messages as $message) {
$data[] = array(
'field' => $message->getField(),
'type' => $message->getType(),
'message' => $message->getMessage()
);
}
return $this->response->setStatusCode(409, 'Conflict')->setJsonContent(array('error' => $data));
}
return $this->response->setStatusCode(201, 'Created')->setJsonContent(array('message' => "User id:" . $user->getId() . " created"));
}
当我用类似的东西更改此 for 循环时,我得到空响应:
return $this->response->setStatusCode(409, 'Conflict')->setJsonContent(array('error' => $user->getMessages()));
问题是有没有可能使这段代码更短而不通过消息循环,但只是简单的 return json 对象。
您可以通过扩展默认方法进行很少的修改就可以做到这一点。这是您的代码的工作示例:
// BaseModel class which overwrites default Phalcon methods if needed.
class BaseModel extends \Phalcon\Mvc\Model
{
public function getMessagesNormalized()
{
$messages = parent::getMessages();
$data = [];
foreach ($messages as $message) {
$data[] = array(
'field' => $message->getField(),
'type' => $message->getType(),
'message' => $message->getMessage()
);
}
return $data;
}
}
// Note that your models from now on extend BaseModel which extends \Phalcon\Mvc\Model
class Users extends BaseModel {
// ...
}
// Your controller
class UserController extends Phalcon\Mvc\Controller {
public function register() {
$data = $this->request->getJsonRawBody();
$user = new Users();
$user->setEmail($data->email)->setUsername($data->username)->setPassword($data->password);
if ($user->save() === false) {
$data = [];
// Get the messages in RESTsuitable format
$messages = $user->getMessagesNormalized();
return $this->response->setStatusCode(409, 'Conflict')->setJsonContent(array('error' => $data));
}
return $this->response->setStatusCode(201, 'Created')->setJsonContent(array('message' => "User id:" . $user->getId() . " created"));
}