問(wèn)題描述
我正在嘗試將一個(gè)變量從一個(gè)視圖傳遞到另一個(gè)視圖的控制器.我沒(méi)有收到任何錯(cuò)誤,但是當(dāng)它進(jìn)入最后一個(gè)視圖時(shí),它沒(méi)有像預(yù)期的那樣顯示變量.在第一個(gè)視圖中,我只是得到一個(gè)名字.
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() }}
這是我的 HomeController.php.
Here is my 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>
那為什么不顯示呢?
推薦答案
首先你應(yīng)該把你的 postView
函數(shù)改成:
First you should change your postView
function into:
public function postView1()
{
return Redirect::route('view2', ['name' => Input::get('name')]);
}
還有你的路線:
Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController@postView1'));
進(jìn)入:
Route::post('form', array('as' => 'form', 'uses'=>'HomeController@postView1'));
現(xiàn)在,您應(yīng)該將 view2
函數(shù)更改為:
Now, you should change your view2
function into:
public function view2($name)
{
return View::make('view2')->with('name',$name);
}
現(xiàn)在在您的 view2.blade.php
中,您應(yīng)該可以使用:
Now in your view2.blade.php
you should be able to use:
<p> Hello, {{ $name }} </p>
這篇關(guān)于將變量從控制器傳遞到視圖 - Laravel的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!