簡體   English   中英

如何在 laravel 帖子中自定義 url/slug

[英]How to customize url/slug in laravel post

我試圖讓我的網站 slug/url 像這樣localhost:8000/apartment/example-post-cat-come而不是localhost:8000/example-post-cat-come

我有像 1. 公寓 2. 公寓這樣的類別

我想在 url 中的 slug 之前添加帖子類別

目前我正在這樣做

Route::get('/{post}','ShowPropertiesController@index')->name('posts.show');

在嘗試了第一條路線后,我也做了以下事情

Route::get('{category}/{post}','ShowPropertiesController@index')->name('posts.show');

並在我的 controller

public function index($category, $post)
{
    // ...
}

我收到此錯誤Missing required parameters for [Route: posts.show] [URI: {category}/{post}]. (View: C:\MAMP\htdocs\laravel-real-estate\resources\views\pages\welcome.blade.php) Missing required parameters for [Route: posts.show] [URI: {category}/{post}]. (View: C:\MAMP\htdocs\laravel-real-estate\resources\views\pages\welcome.blade.php)這是我的刀片

<a href="{{ route('posts.show',$properties->slug) }}">{{$properties->title}}</a>

如何在 url 之前顯示類別?

問題出在 welcome.blade.php 中,您應該將所有必需的參數傳遞給路由:

<a href="{{ route('posts.show',['category' => CATEGORY, 'post' => POST]) }}">{{$properties->title}}</a>

使用您的值更改 CATEGORY 和 POST

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM