Laravel在URL中路由带有id的长slug

时间:2017-10-26 04:33:57

标签: php laravel-5

我想让我的网站(Laravel 5.4)有这样的网址

http://example.com/im-a-stackoverflow-member-102345689.html
                             ^                  ^
                  {--------SLUG-----------}-{post_id}

我创建了这样的路线。

/* Routing for accessing news detail */
Route::get('/{slug}-{article_id}.htm', 'articleController@getArticleDetailById')
    ->name('getArticleDetailById')
    ->where(['slug' => '[A-Za-z]+'])
    ->where(['article_id' => '[0-9]+']);

但是不能用作我的期望网址,上面的代码仅在我访问此类http://example.com/im-102345689.html的链接时才有效。我不能将路由规则用作/,例如{slug}/{id},因为我需要保留所有旧链接。

有没有人有任何想法?

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

将您的路线正则表达式更改为:

->where(['slug' => '[A-Za-z-]+'])

 ->where(['slug' => '[A-Za-z-0-9]+[^-0-9.]+'])
相关问题