Best Controller Approaches

This is a 2 part question about what approach is best. In this context all my code is in Controllers.

Part 1
Firstly, I just read that in a controller one should never used echo, is that true?

For instance, I had

public function actionCreate()
    {
        $model = new ClientsContacts();

        if ($model->load(Yii::$app->request->post())) {
            $model->dtCreation = date('Y-m-d H:i:s');
            if ($model->validate()) {
                $model->save();
                echo json_encode(['status' => 'Success', 'message' => '<span class="glyphicon glyphicon-ok"></span> New entry created successfully.']);
            }else{
                echo json_encode(['status' => 'Error', 'message' => '<span class="glyphicon glyphicon-remove"></span> Model not valid!']);
            }
        } else {
            return $this->renderAjax('create', [
                'model' => $model,
            ]);
        }
    }

and it should be

public function actionCreate()
    {
        $model = new ClientsContacts();

        if ($model->load(Yii::$app->request->post())) {
            $model->dtCreation = date('Y-m-d H:i:s');
            if ($model->validate()) {
                $model->save();
                return json_encode(['status' => 'Success', 'message' => '<span class="glyphicon glyphicon-ok"></span> New entry created successfully.']);
            }else{
                return json_encode(['status' => 'Error', 'message' => '<span class="glyphicon glyphicon-remove"></span> Model not valid!']);
            }
        } else {
            return $this->renderAjax('create', [
                'model' => $model,
            ]);
        }
    }

Part 2
I use certain actionGet… with Ajax to update Selects. The code that I found originally was

public function actionGetContactInfo($id){
    $countContacts = ClientsContacts::find()
        ->where(['ContactId' => $id])
        ->count();
    if( $countContacts > 0 ){
        $Contact = ClientsContacts::findOne($id);
        return $Contact->ContactInfo;
    }
}

is there any reason I can’t simply use:

public function actionGetContactInfo($id){
    $output = '';

    $contact = ClientsContacts::findOne($id);
    if( $contact ){
        $output .= $contact ->ContactInfo;
    }

    return $output;
}

Just seem odd to use to call, one for a counter and then another to get the actual data.

This is all very new to me, so if you are inclined, please explain so I can learn.

I truly appreciate your insight into these issues!

Hi There,

For Part - 1 when you try to render JSON use proper response headers!.

\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

For Part -2 It depends! Either you can use proper response headers nor use simply concatenating as you wish!. But I suggest to use proper try catch or proper response code .

For More info : https://www.yiiframework.com/doc/guide/2.0/en/runtime-responses

So for 1, use return, for instance

\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return json_encode(['status' => 'Success', 'message' => '<span class="glyphicon glyphicon-ok"></span> New entry created successfully.']);

Does the same apply to model attributes? Such as below, should I be including a response format as well, if so which one?

public function getFullName()
    {
        $output = $this->FirstName;
        if( isset($this->LastName) && trim($this->LastName) != '' ){
            $output .= ' '.$this->LastName;
        }
        return $output;
    }

It’s a plain text value being returned, should I use RAW or HTML (even though I am not using any HTML tags actively)?

As for 2, could you elaborate, I’d love to learn.

  1. Yes, don’t echo from the controller. Set the response header to JSON, as mentioned above, and return the JSON string.

  2. You don’t have to use the count() method. You can count a returned array from the all() method
    That said, your use case is a little odd. You count it (to see if there is one, or is it suppose to check for multiple?), but then you only return one record. The findOne method returns null if no records are found.

if(($contact = ClientContacts::findOne(['id' => $id])) !== null) {
    return $contact->ContactInfo; // or whatever you need to do here
}

// do the return output here I guess? I don't really know what you're doing