Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 - Get Current Route Name on Hidden Input to use for search

I have a search form in the nav bar on my Laravel 4 app. When someone types into search and submits I want the queries to only be performed based on what section of the site they are on. Example, if they are on the blog page, I want it to return only blog posts, and if they are on the users page, I want it to only bring up a list of users.

Unless I'm missing something (certainly possibly as I'm new to Laravel!) I can do this using getCurrentRoute()->getPath(). But I have to do this from the form, because when I try to do it in a Controller it does not have the old route any more. So, I created a hidden field on my form like so:

{{ Form::hidden('route', '{{{Route::getCurrentRoute()->getPath()}}}') }};

However, the "getCurrentRoute()->getPath()" is never evaluated properly. The form field literally prints it out:

<input name="route" type="hidden" value="&lt;?php echo e(Route::getCurrentRoute()-&gt;getPath()); ?&gt;">

Does anyone know a way to get this to work, or is there a better way to do this that I'm totally missing? Again, I can't insert this into a controller like normal because at that point the route has been sent to

// Search
Route::post('search', array('as' => 'search', 'uses' => 'SearchController@postSearch'), function(){
});

At which point it always thinks the route name is "search".

like image 301
user2444007 Avatar asked Jun 01 '13 20:06

user2444007


2 Answers

Try this:

{{ Form::hidden('route', Route::getCurrentRoute()->getPath()) }}

Because it is already inside a PHP block..

like image 175
Antonio Carlos Ribeiro Avatar answered Oct 01 '22 23:10

Antonio Carlos Ribeiro


If you are looking for the named route then use:

Route::currentRouteName()

Would return search for the example you gave.

Relevant documentation http://laravel.com/docs/routing#named-routes

like image 40
Mike Grace Avatar answered Oct 01 '22 23:10

Mike Grace