Laravel 4-在隐藏输入中获取当前路线名称以用于搜索 [英] Laravel 4 - Get Current Route Name on Hidden Input to use for search

查看:50
本文介绍了Laravel 4-在隐藏输入中获取当前路线名称以用于搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 4应用程序的导航栏中有一个搜索表单.当有人键入搜索并提交时,我希望仅根据他们所在网站的哪个部分来执行查询.例如,如果他们在博客页面上,我希望它只返回博客文章,如果他们在用户页面上,我希望它只显示用户列表.

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的新手!),我可以使用getCurrentRoute()-> getPath()来做到这一点.但是我必须从表单中执行此操作,因为当我尝试在Controller中执行操作时,它不再具有旧的路由.因此,我在表单上创建了一个隐藏字段,如下所示:

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()}}}') }};

但是,永远不会正确评估"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".

推荐答案

尝试一下:

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

因为它已经在PHP块中.

Because it is already inside a PHP block..

这篇关于Laravel 4-在隐藏输入中获取当前路线名称以用于搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆