I've to authorize some users to do POST requests from their CMSs in order to create a new resource on my database. I've to send a JSON response containing the status of the response (successful or not), and on failure, send an array of the errors.To not continuously repeat the return statement (i.e. return response->json($response)), I decided to use goto. Is it good or bad?class ApiController extends Controller{    public function sendRequest(Request $request){        $rules = [            'username' => 'required',            'password' => 'required',            //other rules        ];        // Initialize response array        $response['success'] = false;        // Validate the input        $validator = Validator::make($request->all(), $rules);        if ($validator->fails()){            $response['errors'] = $validator->getMessageBag()->toArray();            goto send_response;        }        $username = $request->input('username');        $password = $request->input('password');        $user = UserAuth::where('username', $username)->first();        // Check if user exists        if (null === $user){            $response['errors']['username'] = 'Invalid Username';            goto send_response;        }        // Check if password matches        if (!Hash::check($password, $user->password)){            $response['errors']['password'] = 'Invalid Password';            goto send_response;        }        // other code here, at the end set $response['success'] to true        send_response:         return response()->json($response);    }}
Authorizing users for POST requests
php;php5;laravel
I do not see much thing to review, except goto.Code style......is OK.I will suggest functions to be no more than 10-15 lines, unless you doing something that can not be separated, such preparing parameters for SQL database or things like this. goto issueYou should avoid goto at all costs. Remember in PHP there are operators such break 2, here is an example:for($i = 0; $i < 100; ++$i)   for($j = 0; $j < 100; ++$j){      // do something      if ($some_condition)         break 2; // exit two loops at once.   }In your case, I would suggest to do two functions, and instead of goto, use return. I can not be sure if this code will work, but this is the idea:class ApiController extends Controller{    private function prepareResponse_(Request $request){        $rules = [            'username' => 'required',            'password' => 'required',            //other rules        ];        // Initialize response array        $response['success'] = false;        // Validate the input        $validator = Validator::make($request->all(), $rules);        if ($validator->fails()){            $response['errors'] = $validator->getMessageBag()->toArray();            return $response;        }        $username = $request->input('username');        $password = $request->input('password');        $user = UserAuth::where('username', $username)->first();        // Check if user exists        if (null === $user){            $response['errors']['username'] = 'Invalid Username';            return $response;        }        // Check if password matches        if (!Hash::check($password, $user->password)){            $response['errors']['password'] = 'Invalid Password';            return $response;        }        // other code here, at the end set $response['success'] to true        return $response;        }    public function sendRequest(Request $request){        $response = prepareResponse_($request);        return response()->json($response);    }}Please note, many people (me excluded) will not like return in the middle of function.Anyway if you do it this way, readability will be much better than with goto.