CakePHP login action returning json I have some questions on how I can improve this "add action" (method) in "controller":


I'm using the add action only if post request. Is it correct?
This action doesn't have views ($this-&gt;autoRender = false;). Is it correct?
I set a response .json file to this action but I didn't change on routes to routing .json files (the file will be return when access localhost:8765/users/add). Is it correct?
I'm using Enums(handmade) to store messages that will returned to user. Is it correct?
I'm using an object to store the fields of message (that will returned to user), that object will be serialized and returned like this:

$this-&gt;response-&gt;body(json_encode($response));


Is it correct?


Controller code:

public function login()
{
    $this-&gt;autoRender = false;
    $this-&gt;response-&gt;type('json');
    if ($this-&gt;request-&gt;is('post')) {
        $user = $this-&gt;Auth-&gt;identify();
        if ($user) {
            $this-&gt;Auth-&gt;setUser($user);
            $response = new ResponseMessage();
            $response-&gt;code = CodeEnum::LOGIN_GRANTED;
            $response-&gt;name = NameEnum::LOGIN_GRANTED;
            $response-&gt;type = TypeMessageEnum::SUCCESS;
            $this-&gt;response-&gt;body(json_encode($response));
        }else {
            $response = new ResponseMessage();
            $response-&gt;code = CodeEnum::LOGIN_DENIED;
            $response-&gt;name = NameEnum::LOGIN_DENIED;
            $response-&gt;message = MessageEnum::USER_PASS_INCORRECT;
            $response-&gt;type = TypeMessageEnum::ERROR;
            $this-&gt;response-&gt;body(json_encode($response));
        }
    }
}
If you have another point to improve, please comment or answer, I will change the accepted answer


To put in CakePHP pattern I remove both lines:

$this-&gt;autoRender = false;
$this-&gt;response-&gt;type('json');


and change how I create and return the JSON file change this:

$this-&gt;response-&gt;body(json_encode($response));


to this:

$this-&gt;set('response', $response);
$this-&gt;set('_serialize', 'response');


the result:

public function login()
{
    if ($this-&gt;request-&gt;is('post')) {
        $user = $this-&gt;Auth-&gt;identify();
        if ($user) {
            $this-&gt;Auth-&gt;setUser($user);
            $response = new ResponseMessage();
            $response-&gt;code = CodeEnum::LOGIN_GRANTED;
            $response-&gt;name = NameEnum::LOGIN_GRANTED;
            $response-&gt;type = TypeMessageEnum::SUCCESS;
            $this-&gt;set('response', $response);
            $this-&gt;set('_serialize', 'response');
        }else {
            $response = new ResponseMessage();
            $response-&gt;code = CodeEnum::LOGIN_DENIED;
            $response-&gt;name = NameEnum::LOGIN_DENIED;
            $response-&gt;message = MessageEnum::USER_PASS_INCORRECT;
            $response-&gt;type = TypeMessageEnum::ERROR;
            $this-&gt;set('response', $response);
            $this-&gt;set('_serialize', 'response');
        }
    }
}