【问题标题】:Laravel redirect back not passing variableLaravel 重定向回不传递变量
【发布时间】:2014-04-30 17:01:49
【问题描述】:

我正在创建一个简单的页面来使用 md5 对字符串进行哈希处理,但是从不返回输出。

这是我用于这些页面的控制器。 函数 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);
    }

}

这就是风景

{{ 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() }}

在我的模板文件中,总是显示输入,但是,变量 $output 总是未定义。我一直在尝试使用其他变量名,但它不起作用。

如果我在重定向之前返回变量,我会看到正确的输出。

【问题讨论】:

  • 为什么不使用简单的with 而不是 withOutput ?
  • 两个都试过了,还是不行
  • 我不明白。我没有看到任何输出。它必须得到结果:Redirect::back()-&gt;with-&gt;($hash);
  • 我添加了我使用的视图

标签: php laravel controller


【解决方案1】:

我找到了解决办法。 在我看来,我必须使用Session::get() 来获取价值。

这仍然没有返回正确的输出,但我通过将此变量转换为字符串来得到它。

我的解决方案:

{{ (string)Session::get('output') }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多