【问题标题】:how to change result of a serch to friendly url如何将搜索结果更改为友好的 url
【发布时间】:2021-03-22 15:58:36
【问题描述】:

如何更改提交表单时生成的 URL

http://localhost:8000/list_entreprise?activity=3&sub_category=1&secteur=1&page=2

到这个网址:

http://localhost:8000/list_entreprise/category_name/sub_category_name/secteur_name/2

需要使 URL 对搜索引擎更友好。

我从表单中的选择字段中获取 categorysub_categorysecteur

http://localhost:8000/list_entreprise

我的路线:

Route::get('/list_entreprise','App\Http\Controllers\SiteController@search')->name('search');

【问题讨论】:

    标签: laravel forms seo


    【解决方案1】:

    您需要在路由定义中指定这些组件。

    Route::get('/list_entreprise/{list_entreprise}/{category_name}/{sub_category_name}/{secteur_name}/{page?}','App\Http\Controllers\SiteController@search')->name('search');

    然后一定要将这些作为参数添加到SiteController中的search方法中,即

    public function search($listEnterprise, $categoryName, $subCategoryName, $secteurName, $page = null){}

    注意“?”在page 参数之后,这表示它是URL 的可选组件。

    在此处查看文档https://laravel.com/docs/8.x/routing#route-parameters

    当您为表单的操作指定路由时,您现在需要添加这些参数,以便它可以为您构建 URL。

    【讨论】:

    • 抱歉,这并不能真正回答您的问题。要将这些值替换为从选择框中提交的表单,您需要一个 JavaScript 解决方案,从表单中获取值并将它们替换为表单提交到的 URL。
    • 没有用 @Andy [缺少 [Route: search] 所需的参数] 但谢谢,你知道我怎么能用 Javascripor 任何其他方法做到这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2011-08-31
    • 1970-01-01
    相关资源
    最近更新 更多