Laravel 5.4:Class' App \ Http \ Controllers \ Response'找不到错误

时间:2017-06-12 18:28:44

标签: php laravel rest laravel-5

我正在关注Laracast的API教程并尝试创建所有其他控制器扩展的ApiControllerApiController负责响应处理。

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()
            ]

        ]);
    }
}

我还有一个ReportController延伸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);
    }
}

当我尝试从respondNotFound调用ReportController方法时,我得到了

  

Class' App \ Http \ Controllers \ Response'找不到错误

虽然我将use Illuminate\Support\Facades\Response;添加到父类或子类,但我得到了错误。我该如何解决这个问题?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:22)

由于它是一个立面,请添加:

use Response;

或使用完整命名空间:

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

或者只是使用帮手:

return response()->json(...);