【问题标题】:Laravel 5 - withInput not working when used with redirectLaravel 5 - 与重定向一起使用时 withInput 不起作用
【发布时间】:2016-03-09 21:19:47
【问题描述】:

我一直在尝试做的是将参数作为输入值发送并使用Laravel 5.2 重定向。

我参考了官方文档,Stackoverflow 和 Laracast 上的一些帖子,这是我的尝试。

//This controller function is run by a POST method and gets an input value
public function makeCustomerId()
{
    return redirect('next')
        ->withInput();
}

以下是我的下一次尝试,基于文档。

(输入值可以通过$this->request->input('id', '')获取)

public function makeCustomerId()
{
    return redirect()
         ->action('PageController@showNextPage', ['inputId' => $this->request->input('id', '')]);
}

我使用dd() 函数调试控制器,但两种情况都没有发送任何内容。

你看到我做错了什么吗? 任何建议将不胜感激!

编辑

我添加了更多信息。

上面的控制器函数是从下面的form 运行的。这最终会导致PageController@showNextPage

    <form action="{{url('makeCustomerId')}}" method="POST">
            <select name="id">
                @foreach ($teams as $team)
                <option value="{{$team->id}}">{{$team->id}}</option>
                @endforeach
            </select>
            <br>
            <input type="submit" value="Submit">
    </form>

可能不清楚,但输入参数($this-&gt;request-&gt;input('id', ''))对应$team-&gt;id

下面是我如何使用$this-&gt;request。简而言之,这是依赖注入,这意味着我通过$this-&gt;request从控制器类中的任何位置使用request

use Illuminate\Http\Request;

class PageController extends Controller {
protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

}

【问题讨论】:

  • PageController@showNextPage 动作的路径是什么?是否包含路由参数“inputId”?
  • 感谢您的回复。我添加了更多信息,希望这就是您的意思。
  • $this-&gt;request 到底是什么?您如何分配它以及它来自哪个命名空间?
  • 嗨马尔辛。这就是依赖注入。我会添加更多描述。
  • 两个想法:1) 你确定你能够为 $team->id 加载值吗? 2) 你的 showNextPage 方法是什么,它是否接受输入,然后将其传递给你的下一页视图?

标签: php laravel laravel-5.2


【解决方案1】:

当你打电话时:

return redirect()
     ->action('PageController@showNextPage', ['inputId' => $this->request->input('id', '')]);

您实际上所做的是使用'inputId' 作为输入发出新请求。因此,在您的 showNextPage 方法中,您还需要通过以下方式访问 'inputId'

public function showNextPage()
{
    $id = $this->request->input('inputId');

    // do stuff
    // return next page
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 2015-05-28
    相关资源
    最近更新 更多