Question

I have a route set up like so:

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

In my controller, I'm passing the route parameter to my action like so:

public function resetPasswordConfirm($code)
{
    // ...
}

I can then use $code in my controller as normal.

In my view I'm building a form which POSTs to the same controller action, and I need to somehow get $code into the view so it constructs the proper form action. At the moment I have this:

{{ Form::open(array('route' => array('reset-password-confirm'))) }}

Because I'm not supplying the $code route parameter, the form is opened like this:

<form method="POST" action="http://site.dev/reset-password/%7Bcode%7D" accept-charset="UTF-8">

Obviously, this doesn't match the route I've defined (due to {code} not existing) and route matching fails. I need to somehow get the route parameter into my view so I can use it with Form::open(). I've tried doing this:

{{ Form::open(array('route' => array('reset-password-confirm', $code))) }}

But that just throws an exception saying $code is undefined.

Était-ce utile?

La solution

The proper way to send the parameter to the view is:

return View::make('viewname')->with('code', $code);

Or you may use:

return View::make('yourview', compact('code'));

So, the $code will be available in your view and you may use it in your form but you may also use following approach to access a parameter in the view:

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

// Laravel - 4.0
Route::getCurrentRoute()->getParameter('code');

Autres conseils

Maybe I don't clearly understand your question, but you can pass it to the view regularly (as you would pass any other variable, i.e.)

public function resetPasswordConfirm($code)
{
    return View::make('yourview')->with('code', $code);
}

and in the view $code will be defined:

{{ Form::open(array('route' => array('reset-password-confirm', $code))) }}

or just catch it in your view directly from the Request object:

{{ Form::open(array('route' => array('reset-password-confirm', Request::segment(2) ))) }}

By the way, I think you could write your route also like this:

Route::any('reset-password/{code}', array('as' => 'reset-password-confirm', 'uses' => 'UserController@resetPasswordConfirm'));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top