沃梦达 / 编程问答 / php问题 / 正文

将变量从控制器传递到视图 - Laravel

Passing variable from controller to view - Laravel(将变量从控制器传递到视图 - Laravel)

本文介绍了将变量从控制器传递到视图 - 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将变量从控制器传递到视图 - Laravel

基础教程推荐