13

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

除非我遗漏了什么(当然可能是因为我是 Laravel 的新手!)我可以使用 getCurrentRoute()->getPath() 来做到这一点。但是我必须从表单中执行此操作,因为当我尝试在 Controller 中执行此操作时,它不再具有旧路由。因此,我在表单上创建了一个隐藏字段,如下所示:

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

但是,永远不会正确评估“getCurrentRoute()->getPath()”。表单字段从字面上打印出来:

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

有谁知道让这个工作的方法,或者有没有更好的方法来做到这一点,我完全错过了?同样,我不能像往常一样将它插入到控制器中,因为此时路由已发送到

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

此时它总是认为路线名称是“搜索”。

4

2 回答 2

33

试试这个:

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

因为它已经在 PHP 块中了。

于 2013-06-01T21:08:40.237 回答
19

如果您正在寻找命名路线,请使用:

Route::currentRouteName()

将返回search您给出的示例。

相关文档http://laravel.com/docs/routing#named-routes

于 2014-09-02T01:37:26.043 回答