Laravel 5.4:找不到类'App \ Http \ Controllers \ Response'错误 [英] Laravel 5.4: Class 'App\Http\Controllers\Response' not found error
问题描述
我正在关注Laracast的API教程,并尝试创建所有其他控制器扩展的ApiController
. ApiController
负责响应处理.
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()
]
]);
}
}
我还有一个扩展了ApiController
的ReportController
.
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);
}
}
当我尝试从ReportController
调用respondNotFound
方法时,我得到了
When i try to call respondNotFound
method from ReportController
i get
找不到类'App \ Http \ Controllers \ Response'错误
Class 'App\Http\Controllers\Response' not found error
尽管我将use Illuminate\Support\Facades\Response;
添加到父类或子类中,但我得到了错误.我该如何解决?
eventhough i add use Illuminate\Support\Facades\Response;
to parent or child class i get the error. How can i fix this ?
任何帮助将不胜感激.
推荐答案
由于它是外墙,请添加以下内容:
Since it's a facade, add this:
use Response;
或使用完整的名称空间:
Or use full namespace:
return \Response::json(...);
或仅使用辅助程序:
return response()->json(...);
这篇关于Laravel 5.4:找不到类'App \ Http \ Controllers \ Response'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!