php Laravel 5.4:找不到类“App\Http\Controllers\Response”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44506361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.4: Class 'App\Http\Controllers\Response' not found error
提问by Tartar
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.
我正在关注 Laracast 的 API 教程并尝试创建一个ApiController
所有其他控制器都扩展的。ApiController
负责响应处理。
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
.
我也有一个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);
}
}
When i try to call respondNotFound
method from ReportController
i get
当我尝试respondNotFound
从ReportController
我那里调用方法时
Class 'App\Http\Controllers\Response' not found error
找不到类“App\Http\Controllers\Response”错误
eventhough i add use Illuminate\Support\Facades\Response;
to parent or child class i get the error. How can i fix this ?
尽管我添加use Illuminate\Support\Facades\Response;
到父类或子类,但我收到错误。我怎样才能解决这个问题 ?
Any help would be appreciated.
任何帮助,将不胜感激。
回答by Alexey Mezenin
Since it's a facade, add this:
因为它是一个门面,添加这个:
use Response;
Or use full namespace:
或者使用完整的命名空间:
return \Response::json(...);
Or just use helper:
或者只是使用助手:
return response()->json(...);