Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4: Class 'App\Http\Controllers\Response' not found error

I am following the Laracast's API tutorial and trying to create an ApiController that all the other controllers extend. ApiController is responsible for response handling.

class ApiController extends Controller
{
    protected $statusCode;

    public function getStatusCode()
    {
        return $this->statusCode;
    }

    public function setStatusCode($statusCode)
    {
        $this->statusCode = $statusCode;
    }

    public function respondNotFound($message = 'Not Found!')
    {
        return Reponse::json([

            'error' => [
                'message' => $message,
                'status_code' => $this->getStatusCode()
            ]

        ]);
    }
}

And i also have a ReportController that extends ApiController.

class ReportController extends ApiController
{
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $report = Report::find($id);

        if (! $report ) {
            $this->respondNotFound(Report does not exist.');
        }

        return Response::json([
            'data'=> $this->ReportTransformer->transform($report)
        ], 200);
    }
}

When i try to call respondNotFound method from ReportController i get

Class 'App\Http\Controllers\Response' not found error

eventhough i add use Illuminate\Support\Facades\Response;to parent or child class i get the error. How can i fix this ?

Any help would be appreciated.

like image 895
Tartar Avatar asked Jun 12 '17 18:06

Tartar


1 Answers

Since it's a facade, add this:

use Response;

Or use full namespace:

return \Response::json(...);

Or just use helper:

return response()->json(...);
like image 98
Alexey Mezenin Avatar answered Nov 15 '22 19:11

Alexey Mezenin