本文介紹了將變量從控制器傳遞到視圖 - Laravel的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試將一個變量從一個視圖傳遞到另一個視圖的控制器.我沒有收到任何錯誤,但是當它進入最后一個視圖時,它沒有像預期的那樣顯示變量.在第一個視圖中,我只是得到一個名字.
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>
那為什么不顯示呢?
推薦答案
首先你應該把你的 postView
函數改成:
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'));
進入:
Route::post('form', array('as' => 'form', 'uses'=>'HomeController@postView1'));
現在,您應該將 view2
函數更改為:
Now, you should change your view2
function into:
public function view2($name)
{
return View::make('view2')->with('name',$name);
}
現在在您的 view2.blade.php
中,您應該可以使用:
Now in your view2.blade.php
you should be able to use:
<p> Hello, {{ $name }} </p>
這篇關于將變量從控制器傳遞到視圖 - Laravel的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!