Accessing route parameter in view with Laravel 4
laravel, laravel-4, php, url-routing
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');
Problem
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.