用Phalcon PHP作为json返回验证消息的最佳方法是什么

时间:2019-03-28 18:19:13

标签: php rest validation phalcon phalcon-orm

我正在创建REST API,我想从验证中返回消息。

以下是用户注册的示例:

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()));

问题是有可能使这段代码更短而没有循环低谷消息,而只是简单的返回json对象。

1 个答案:

答案 0 :(得分:2)

您可以通过扩展默认方法进行很少的修改。这是您的代码的工作示例:

// 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"));
}