【问题标题】:Multiple optional parameters not working on laravel 5.3 route多个可选参数不适用于 laravel 5.3 路由
【发布时间】:2018-06-23 19:53:30
【问题描述】:

我在 web.php 中有以下路由,第一个路由总是有效,但如果我使用像这样的 url,第二个则不会

ads/mobiles

然后函数 check_if_category 执行良好。但我使用 url 之类的

ads/lahore/mobiles

在这种情况下,它会重定向到 404 页面。

Route::get('ads/all', 'AdControllerWithoutAuth@all_ads')->name('route_all_ads');
Route::get('ads/{location?}{category?}{keyword?}', 'Categories@check_if_category')->name('route_f_category_page');

我也是这样工作的

Route::get('ads/all', 'AdControllerWithoutAuth@all_ads')->name('route_all_ads');
//Route::get('ads/{location?}', 'Categories@check_if_category')->name('route_f_category_page');
//Route::get('ads/{location?}{category?}', 'Categories@check_if_category')->name('route_f_category_page');
Route::get('ads/{location?}{category?}{keyword?}', 'Categories@check_if_category')->name('route_f_category_page');

但没有成功。提前感谢您的帮助。

【问题讨论】:

    标签: php laravel routes laravel-5.3


    【解决方案1】:

    使用separator

    Route::get('ads/{location?}/{category?}/{keyword?}', 'Categories@check_if_category')->name('route_f_category_page');
    

    【讨论】:

      【解决方案2】:

      / 添加到路由URI:

      Route::get('ads/{location?}/{category?}/{keyword?}', 'Categories@check_if_category')->name('route_f_category_page');
      

      【讨论】:

        【解决方案3】:

        您需要在这些参数之间添加/,否则 Laravel 会将它们视为一个长字符串。

        Route::get('ads/{location?}/{category?}/{keyword?}', 'Categories@check_if_category')->name('route_f_category_page');
        

        【讨论】:

        • 很高兴我能提供帮助,请考虑将此标记为您问题的解决方案,以便其他用户也可以找到它。 :)
        猜你喜欢
        • 2017-01-10
        • 2017-09-30
        • 2014-05-04
        • 2015-02-05
        • 1970-01-01
        • 2015-02-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多