S
S
Sergey Zolotarev2021-12-29 18:17:05
PHP
Sergey Zolotarev, 2021-12-29 18:17:05

How to populate the contents of variables while executing if-else statements?

Good evening!

The action of one of my Yii2 controllers I wrote consists of an implemented microarchitecture web service that only works on JSON.

On the first lines, I initialized this variable:
$serviceResponse = NULL;
And below it are if-else statements that fill the variable separately depending on the user's query parameters.

And my response itself is built like this:

\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ["response" => $serviceResponse];


When I test the service itself in the client part of the project, instead of the received content, this is displayed: {response: []}

What to do with it and how to dynamically fill the variable with any content types without errors (if it is filled only inside if-else)? Thank you in advance!

Code of the problematic action in the controller

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Bay, 2021-12-29
@kawabanga

The code needs to be heavily refactored, it is terribly readable.
For your question specifically
1) look in xdebug to see if the dots work.
2) Why are you working with json like a string?
With response json in yii2, you must return an array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question