【问题标题】:Laravel send query string with optional parametersLaravel 发送带有可选参数的查询字符串
【发布时间】:2018-06-27 07:44:07
【问题描述】:

我的路线设置为

Route::any('/{brand?}/{type?}/{city?}', 'SearchController@index')->name('search');

我想从我的控制器查询字符串(表单 GET 参数)发送

搜索后我得到了这个,但它不能正常工作

return redirect()->route('search', [$brand->name, $type->name, 'search_model_from' => $request->search_model_from, 'search_model_to' => $request->search_model_to]);

返回

localhost:8000/toyota/avalon/2018?search_model_to=2019

我想回来

localhost:8000/toyota/avalon/?search_model_from=2018&search_model_to=2019

一般来说,我想要实现的是 SEO 友好的搜索功能

【问题讨论】:

  • 你能在这里添加你的SearchController@index 方法吗?
  • 也许你应该尝试像这样将城市分配为空:return redirect()->route('search', ['brand' => $brand->name, 'type' => $type->name, 'city' => '', 'search_model_from' => $request->search_model_from, 'search_model_to' => $request->search_model_to]);
  • @JulienMetral 是的,解决了它:) 添加它作为答案,这样我就可以标记为已解决..谢谢你,好先生

标签: laravel routes laravel-5.6


【解决方案1】:

也许你应该尝试像这样将城市分配为空:

return redirect()->route('search', [
    'brand' => $brand->name, 'type' => $type->name, 
    'city' => '', 'search_model_from' => $request->search_model_from, 
    'search_model_to' => $request->search_model_to
]);

【讨论】:

    【解决方案2】:

    我不确定,但这可能会发生,因为您在路由中定义了 3 个可选参数,并且由于您只发送其中两个,这可能会将下一个(在本例中为“search_model_from”)作为第三个参数网址。

    也许如果你在你的控制器中为可选参数强制转换并设置一个默认值,你就不会遇到这样的麻烦,像这样:

    public function index(string $brand='', string $type='', string $city='' , $other_parameters)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-12
      • 2023-02-09
      • 1970-01-01
      • 2017-12-28
      • 2019-08-01
      • 1970-01-01
      • 2011-01-17
      • 2015-06-09
      相关资源
      最近更新 更多