laravel 5:如何通过响应返回视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38776186/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:15:07  来源:igfitidea点击:

laravel 5 : How to return view with response

phplaravellaravel-5.2

提问by Gammer

I have two returnin my controller method.

return我的控制器方法中有两个。

For view :

查看:

return view('user.profile',compact('user','tickets'));

For response :

回应:

return response()->json(array('someData1'=>$someData1,'someData2' => $someData2));

How can I combine both?

我怎样才能将两者结合起来?

回答by jaysingkar

Assuming you want to return the response depending on the request type.
If you want to send the compiled view via your json response please see the @Vojo123 's answer

To achieve this you can first check if the request is ajax or not and then return the proper response.
In your method use following code.

假设您想根据请求类型返回响应。
如果您想通过 json 响应发送编译后的视图,请参阅@Vojo123 的回答

要实现这一点,您可以先检查请求是否为 ajax,然后返回正确的响应。
在您的方法中使用以下代码。

    if($request->ajax()){
         return response()->json(array('someData1'=>$someData1,'someData2' => $someData2));
    }
    return view('user.profile',compact('user','tickets'));