Laravel 4 - 在隐藏输入上获取当前路线名称以用于搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16877121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 - Get Current Route Name on Hidden Input to use for search
提问by user2444007
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.
我的 Laravel 4 应用程序的导航栏中有一个搜索表单。当有人输入搜索并提交时,我希望仅根据他们所在网站的哪个部分来执行查询。例如,如果它们在博客页面上,我希望它只返回博客文章,如果它们在用户页面上,我希望它只显示用户列表。
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:
除非我遗漏了什么(当然可能因为我是 Laravel 的新手!)我可以使用 getCurrentRoute()->getPath() 来做到这一点。但是我必须从表单中执行此操作,因为当我尝试在 Controller 中执行此操作时,它不再具有旧路由。因此,我在表单上创建了一个隐藏字段,如下所示:
{{ Form::hidden('route', '{{{Route::getCurrentRoute()->getPath()}}}') }};
However, the "getCurrentRoute()->getPath()" is never evaluated properly. The form field literally prints it out:
但是,“getCurrentRoute()->getPath()”永远不会被正确评估。表单字段从字面上打印出来:
<input name="route" type="hidden" value="<?php echo e(Route::getCurrentRoute()->getPath()); ?>">
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".
此时它总是认为路线名称是“搜索”。
回答by Antonio Carlos Ribeiro
Try this:
尝试这个:
{{ Form::hidden('route', Route::getCurrentRoute()->getPath()) }}
Because it is already inside a PHP block..
因为它已经在一个 PHP 块中了..
回答by Mike Grace
If you are looking for the named route then use:
如果您正在寻找命名路线,请使用:
Route::currentRouteName()
Would return search
for the example you gave.
会返回search
你给出的例子。
Relevant documentation http://laravel.com/docs/routing#named-routes