简体   繁体   English

如何在Laravel 5.1中通过响应视图传递变量?

[英]How to pass variable with response view in Laravel 5.1?

I am trying to pass variables with response view in Laravel, but it is not allowing me to do so. 我试图在Laravel中使用响应视图传递变量,但不允许这样做。 Here is what I have tried so far: 到目前为止,这是我尝试过的:

return response(view('home'))->with('name','rocky');

the above method doesn't work, 上面的方法不起作用,

the second one I tried is as follow: 我尝试的第二个如下:

return response(view('home'), ['name','rocky']);

but I don't know how to use the above one? 但是我不知道如何使用上面的那个?

NOTE: I don't want to use view::make method, I want to pass variable with response method. 注意:我不想使用view :: make方法,我想通过response方法传递变量。

The view is not passed in as a parameter to response() , it is called as a method on response() . 视图不会传递作为参数来response()它被称为为方法上response() So, you need to change your code to: 因此,您需要将代码更改为:

return response()->view('home', ['name' => 'rocky']);

If you don't actually need the full response object (you don't need to modify headers or anything), you can just return the view object using the view() helper method: 如果您实际上不需要完整的响应对象(不需要修改标头或其他任何内容),则可以使用view()帮助方法返回视图对象:

return view('home', ['name' => 'rocky']);

You can read more in the documentation here . 您可以在此处阅读更多文档

看看Laravel文档

return response()->view('hello', ['name' => 'rocky']);

Try to use the view method itself. 尝试使用视图方法本身。 Eg: 例如:

$name = 'rocky';
return view('home', compact('name'));

My suggestion is you read the Laravel documentation thoroughly, and master the basics of PHP. 我的建议是您彻底阅读Laravel文档,并掌握PHP的基础知识。 ( http://laravel.com/docs/5.1 ) http://laravel.com/docs/5.1

You can try this: 您可以尝试以下方法:

$name = 'rocky';
return view('resizeImage',compact('name'));

Basically reponse() will help you to customize the response's HTTP status code and headers so no need to use it. 基本上, reponse()将帮助您自定义响应的HTTP状态代码和标头,因此无需使用它。 See Documentation 请参阅说明文件

Hope this work for you! 希望这项工作对您有帮助!

You can try like that, 你可以这样尝试

$name = 'rocky';
return view("folder_name.blade_name", compact("name'));

In your case, blade_name will be home and check if there is any folder above it, inside resources folder. 在您的情况下, blade_nameblade_name home并检查其上方在resources文件夹中是否有任何文件夹。

I hope this will work. 我希望这会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM