Laravel 5 - withInput 在与重定向一起使用时不起作用

Laravel 5 - withInput not working when used with redirect

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

我参考了官方文档,Whosebug和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() 函数调试控制器,但两种情况都没有发送任何内容。

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

编辑

我补充一些信息。

上面的controller函数是下面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->request->input('id', ''))对应的是$team->id.

下面是我如何使用$this->request。简而言之,这是依赖注入,这意味着我在控制器 class 的任何地方通过 $this->request

使用 request class
use Illuminate\Http\Request;

class PageController extends Controller {
protected $request;

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

}

当你打电话时:

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
}