dimanche 24 juillet 2016

How to send a response from a method that is not the controller method?

I've got a Controller.php whose show($id) method is hit by a route.

public function show($id)
{
    // fetch a couple attributes from the request ...

    $this->checkEverythingIsOk($attributes);

    // ... return the requested resource.
    return $response;
}

Now, in checkEverythingIsOk(), I perform some validation and authorization stuff. These checks are common to several routes within the same controller, so I'd like to extract these checks and call the method everytime I need to perform the same operations.

The problem is, I'm unable to send some responses from this method:

private function checkEverythingIsOk($attributes)
{
    if (checkSomething()) {
        return response()->json('Something went wrong'); // this does not work - it will return, but the response won't be sent.
    }

    // more checks...
    return response()->callAResponseMacro('Something else went wrong'); // does not work either.

    dd($attributes); // this works.
    abort(422); // this works too.
}

Note: Yes, I know in general one can use middleware or validation services to perform the checks before the request hits the controller, but I don't want to. I need to do it this way.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2a2Bl90
via IFTTT

Aucun commentaire:

Enregistrer un commentaire