【问题标题】:How to customize url/slug in laravel post如何在 laravel post 中自定义 url/slug
【发布时间】:2020-10-02 18:08:17
【问题描述】:

我正试图让我的网站 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');

并在我的控制器中完成此操作

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) 这是我的刀

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

如何让分类显示在 url 之前?

【问题讨论】:

  • 如果第二条路线相同,为什么不能删除第一条路线(供您描述)?
  • 我认为问题出在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)
  • @RainDev 这是我的刀片&lt;a href="{{ route('posts.show',$properties-&gt;slug) }}"&gt;{{$properties-&gt;title}}&lt;/a&gt;

标签: php laravel


【解决方案1】:

问题出在welcome.blade.php,你应该将所有必需的参数传递给路由:

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

使用您的值更改 CATEGORY 和 POST

【讨论】:

  • 它的显示是这样的localhost:8000/Apartment/example-post,但是我怎样才能将Apartment中的A更改为小写字母a这样localhost:8000/apartment/example-post
  • 也许你可以使用php函数['category' =&gt; strtolower(CATEGORY)]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-16
  • 2019-08-01
  • 2021-09-21
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 2015-07-22
相关资源
最近更新 更多