php 将变量从控制器传递到视图 - Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26353030/
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
Passing variable from controller to view - Laravel
提问by porcupine92
I'm trying to pass a variable from one view to a controller to another view. I'm not getting any errors, but when it gets to the last view, it doesn't show the variable like it's supposed to. In the first view, I'm just getting a name.
我试图将一个变量从一个视图传递到一个控制器到另一个视图。我没有收到任何错误,但是当它进入最后一个视图时,它没有像预期的那样显示变量。在第一个视图中,我只是得到一个名字。
{{ Form::open(array('route' => 'form', 'method'=>'post')) }}
{{ $name = Form::text('name') }}
{{ Form::submit('Go!') }}
{{ Form::close() }}
Here is my HomeController.php.
这是我的 HomeController.php。
public function view1()
{
return View::make('stuff');
}
public function postView1($name)
{
return Redirect::route('view2')->with($name);
}
public function view2($name)
{
return View::make('view2')->with($name);
}
routes.php
路由文件
Route::get('/', array('as' => 'stuff', 'uses' => 'HomeController@stuff'));
Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController@postView1'));
Route::get('view2/{name}', array('as' => 'view2', 'uses' => 'HomeController@view2'));
view2.blade.php
view2.blade.php
{{ $name = Input::get('name') }}
<p> Hello, {{ $name }} </p>
So why isn't it showing up?
那为什么不显示呢?
回答by Marcin Nabia?ek
First you should change your postView
function into:
首先,您应该将postView
函数更改为:
public function postView1()
{
return Redirect::route('view2', ['name' => Input::get('name')]);
}
And your route:
还有你的路线:
Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController@postView1'));
into:
进入:
Route::post('form', array('as' => 'form', 'uses'=>'HomeController@postView1'));
Now, you should change your view2
function into:
现在,您应该将view2
函数更改为:
public function view2($name)
{
return View::make('view2')->with('name',$name);
}
Now in your view2.blade.php
you should be able to use:
现在,您view2.blade.php
应该可以使用:
<p> Hello, {{ $name }} </p>
回答by Laurence
You need to name the variable:
您需要命名变量:
public function view2($name)
{
return View::make('view2')->with('name', $name);
}
回答by Sumesh Ps
class HomeController extends Controller {
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
}
public function index()
{
$data = array (
'title'=>'My App yo',
'Description'=>'This is New Application',
'author'=>'foo'
);
return view('home')->with($data);;
}
}
回答by Rakesh Sharma
Try form will be if you using POST method why setting variable in route it will get directly on your function with post data.
如果您使用 POST 方法,请尝试表单,为什么在路由中设置变量,它将直接通过发布数据获取您的函数。
{{ Form::open(array('url' => 'form', 'method'=>'post')) }}
{{Form::text('name') }}
{{ Form::submit('Go!') }}
{{ Form::close() }}
route :-
路线 :-
Route::post('form','HomeController@postView1');
controller function :-
控制器功能:-
public function postView1() {
$data = Input::all();
return Redirect::route('view2')->with('name', $data['name']);
}
and get data on view2 :-
并获取有关 view2 的数据:-
<p> Hello, {{ $name }} </p>
For more follow HERE
更多请关注这里
回答by cha-cha
Here's what other answers are missing, straight from Laravel docs:
以下是其他缺少的答案,直接来自Laravel 文档:
Since the withmethod flashes data to the session, you may retrieve the data using the typical Session::getmethod.
由于with方法将数据闪存到会话中,您可以使用典型的Session::get方法检索数据。
So instead of {{$name}}
write {{Session::get('name')}}
.
所以而不是{{$name}}
写{{Session::get('name')}}
.