我的 Laravel 4 应用程序的导航栏中有一个搜索表单。当有人输入搜索并提交时,我希望仅根据他们所在网站的哪个部分来执行查询。例如,如果它们位于博客页面上,我希望它仅返回博客文章,如果它们位于用户页面上,我希望它仅显示用户列表。
除非我遗漏了一些东西(当然可能是因为我是 Laravel 的新手!),我可以使用 getCurrentRoute()->getPath() 来完成此操作。但我必须从表单中执行此操作,因为当我尝试在 Controller 中执行此操作时,它不再具有旧的路线。因此,我在表单上创建了一个隐藏字段,如下所示:
{{ Form::hidden('route', '{{{Route::getCurrentRoute()->getPath()}}}') }};
但是,“getCurrentRoute()->getPath()”从未被正确评估。表单字段按字面意思打印出来:
<input name="route" type="hidden" value="<?php echo e(Route::getCurrentRoute()->getPath()); ?>">
有谁知道一种方法可以让它发挥作用,或者有没有更好的方法来做到这一点,而我完全不知道?同样,我无法像平常一样将其插入 Controller ,因为此时路由已发送到
// Search
Route::post('search', array('as' => 'search', 'uses' => 'SearchController@postSearch'), function(){
});
此时它始终认为路线名称是“search”。
最佳答案
试试这个:
{{ Form::hidden('route', Route::getCurrentRoute()->getPath()) }}
因为它已经在 PHP block 内..
关于Laravel 4 - 获取隐藏输入上的当前路由名称以用于搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16877121/