使用Laravel 4访问视图中的路径参数

Joh*_*ean 2 php url-routing laravel laravel-4

我有一条路线设置如下:

Route::match(array('GET', 'POST'), '/reset-password/{code}', array('as' => 'reset-password-confirm', 'uses' => 'UserController@resetPasswordConfirm'));
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我将路由参数传递给我的动作,如下所示:

public function resetPasswordConfirm($code)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

然后我可以$code正常使用我的控制器.

在我看来,我正在构建一个POST到同一控制器动作的表单,我需要以某种方式$code进入视图,以便构造正确的表单动作.目前我有这个:

{{ Form::open(array('route' => array('reset-password-confirm'))) }}
Run Code Online (Sandbox Code Playgroud)

因为我没有提供$coderoute参数,所以表单打开如下:

<form method="POST" action="http://site.dev/reset-password/%7Bcode%7D" accept-charset="UTF-8">
Run Code Online (Sandbox Code Playgroud)

显然,这与我定义的路由(由于{code}不存在)不匹配,路由匹配失败.我需要以某种方式将route参数放入我的视图中,以便我可以使用它Form::open().我试过这样做:

{{ Form::open(array('route' => array('reset-password-confirm', $code))) }}
Run Code Online (Sandbox Code Playgroud)

但这只是抛出了一个异常,说$code是未定义的.

The*_*pha 12

发送parameter到视图的正确方法是:

return View::make('viewname')->with('code', $code);
Run Code Online (Sandbox Code Playgroud)

或者您可以使用:

return View::make('yourview', compact('code'));
Run Code Online (Sandbox Code Playgroud)

因此,$code您的视图中可以使用它,您可以在表单中使用它,但您也可以使用以下方法访问parameter视图中的a:

// Laravel - Latest (use any one)
Route::Input('code');
Route::current()->getParameter('code');
Route::getCurrentRoute()->getParameter('code');

// Laravel - 4.0
Route::getCurrentRoute()->getParameter('code');
Run Code Online (Sandbox Code Playgroud)