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

旧巷老猫 提交于 2019-12-08 15:00:50

问题


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".


回答1:


Try this:

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

Because it is already inside a PHP block..




回答2:


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



来源:https://stackoverflow.com/questions/16877121/laravel-4-get-current-route-name-on-hidden-input-to-use-for-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!