php 响应::json() - Laravel 5.1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31865493/
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
Response::json() - Laravel 5.1
提问by senty
I am trying to return Response::json('data', $request);
however, I am getting an error:
return Response::json('data', $request);
但是,我正在尝试,但出现错误:
FatalErrorException in ProjectsController.php line 74: Call to undefined method Illuminate\Http\Response::json()
ProjectsController.php 第 74 行中的 FatalErrorException:调用未定义的方法 Illuminate\Http\Response::json()
Where is the Response::json()
is located? What am I doing wrong?
在哪里Response::json()
位于?我究竟做错了什么?
回答by baao
use the helper function in laravel 5.1instead:
改用laravel 5.1 中的辅助函数:
return response()->json(['name' => 'Abigail', 'state' => 'CA']);
This will create an instance of \Illuminate\Routing\ResponseFactory
. See the phpDocs for possible parameters below:
这将创建一个\Illuminate\Routing\ResponseFactory
. 有关以下可能的参数,请参阅 phpDocs:
/**
* Return a new JSON response from the application.
*
* @param string|array $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Symfony\Component\HttpFoundation\Response
* @static
*/
public static function json($data = array(), $status = 200, $headers = array(), $options = 0){
return \Illuminate\Routing\ResponseFactory::json($data, $status, $headers, $options);
}
回答by Atiqur
After enough googling I found the answer from controller you need only a backslash like return \Response::json(['success' => 'hi, atiq']);
. Or you can just return the array return array('success' => 'hi, atiq');
which will be rendered as json in Laravel version 5.2 .
经过足够的谷歌搜索后,我从控制器中找到了答案,您只需要像return \Response::json(['success' => 'hi, atiq']);
. 或者你可以只返回return array('success' => 'hi, atiq');
将在 Laravel 5.2 版中呈现为 json的数组。
回答by RASEL RANA
You need to add use Response;
facade in header at your file.
您需要use Response;
在文件的标题中添加外观。
Only then you can successfully retrieve your data with
只有这样,您才能成功检索数据
return Response::json($data);
回答by Wireblue
From a controller you can also return an Object/Array and it will be sent as a JSON response (including the correct HTTP headers).
您还可以从控制器返回一个对象/数组,它将作为 JSON 响应(包括正确的 HTTP 标头)发送。
public function show($id)
{
return Customer::find($id);
}
回答by Kelvin Ukuejubola Oritsetimeyi
although Response::json()
is not getting popular of recent, that does not stop you and Me from using it.
In fact you don't need any facade to use it,
虽然Response::json()
最近不太流行,但这并不妨碍你和我使用它。事实上,你不需要任何外观来使用它,
instead of:
代替:
$response = Response::json($messages, 200);
Use this:
用这个:
$response = \Response::json($messages, 200);
with the slash, you are sure good to go.
用斜线,你肯定很高兴。
回答by Kelvin Ukuejubola Oritsetimeyi
However, the previous answer could still be confusing for some programmers. Most especially beginners who are most probably using an older book or tutorial. Or perhaps you still feel the facade is needed. Sure you can use it. Me for one I still love to use the facade, this is because some times while building my api I forget to use the '\' before the Response.
然而,对于一些程序员来说,之前的答案可能仍然令人困惑。尤其是初学者,他们最有可能使用较旧的书籍或教程。或者你仍然觉得需要立面。你当然可以使用它。对于我来说,我仍然喜欢使用外观,这是因为有时在构建我的 api 时我忘记在 Response 之前使用“\”。
if you are like me, simply add
如果你和我一样,只需添加
"use Response;"
above your class ...extends contoller. this should do.
在你的班级之上......扩展控制器。这应该做。
with this you can now use:
有了这个,您现在可以使用:
$response = Response::json($posts, 200);
instead of:
代替:
$response = \Response::json($posts, 200);