Laravel 重定向回不传递变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22590765/
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
Laravel redirect back not passing variable
提问by Jerodev
I am creating a simple page to hash strings with md5, however the output is never returned.
我正在创建一个简单的页面来使用 md5 散列字符串,但是永远不会返回输出。
This is the controller I use for these pages. The function md5 is routed to the get and the function md5post is routed to the post. The view has a form that is posted to the md5post function. it has one variable $input with the string to hash.
这是我用于这些页面的控制器。函数 md5 被路由到 get,函数 md5post 被路由到 post。该视图有一个发布到 md5post 函数的表单。它有一个变量 $input 和要散列的字符串。
<?php
class ConversionsController extends \BaseController {
private $withmd5;
public function __construct(){
$this->withmd5 = [
'pagetitle' => 'MD5 hashing',
'description' => 'description',
'infoWindow' => 'info',
'btnSumbit' => Form::submit('Hash', ['class' => 'btn btn-default'])
];
}
public function md5(){
return View::make("layout.textareamaster")->with($this->withmd5);
}
public function md5post(){
if (strlen(Input::get("input")) > 0)
{
$hash = md5(Input::get("input"));
}
return Redirect::back()->withInput()->with("output", $hash);
}
}
And this is the view
这是视图
{{ Form::open(['method' => 'post']) }}
<div class="row">
<div class="col-md-6">
<p class="well">
{{ Form::textarea('input', '', ['class' => 'form-control', 'rows' => 5]) }}
<span style="float:right;">
{{ $btnSubmit or Form::submit('Go', ['class' => 'btn btn-default']) }}
</span>
</p>
</div>
<div class="col-md-6">
<p class="well">
<textarea class="form-control" rows="5" readonly="true">{{ $output or "nothing" }}</textarea>
</p>
</div>
</div>
{{ Form::close() }}
When in my template file, the input is always displayed, however, the variable $output is always undefined. I have beent trying to use other variable names, but it wont work.
在我的模板文件中,总是显示输入,但是,变量 $output 总是未定义。我一直在尝试使用其他变量名,但它不起作用。
If I return the variable right before the redirect, I see the correct output.
如果我在重定向之前返回变量,我会看到正确的输出。
回答by Jerodev
I have found a solution.
In my view I had to use Session::get()
to get the value.
我找到了解决办法。在我看来,我必须使用它Session::get()
来获取价值。
That still didn't return the correct output, but I got it by casting this variable to string.
那仍然没有返回正确的输出,但我通过将此变量转换为字符串得到了它。
My solution:
我的解决方案:
{{ (string)Session::get('output') }}