繁体   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