Laravel 路由器没有 / ,路由有 {slug}-something

Laravel router with no / , route with {slug}-something

我有以下路线:

Route::get(
    '{slug}-lawyer-in-nepal',
    array(
        'as' => 'practise-area.detail',
        'uses' => 'PractiseAreaController@detail'
    )
);

当 slug 不包含“-”时有效,但当有时抛出 404。对此有什么解决方案吗?

使用 Regex 指定 slug 的格式可能有助于路由器解析您的 url。

routes/web.php

Route::get('{slug}-lawyer-in-nepal', [
    'as' => 'practise-area.detail',
    'uses' => 'PractiseAreaController@detail'
])->where('slug', '[\w-]{1,}[^-]');

您可以查看 Laravel Routing - Regular Expression Constraints documentation 了解更多信息。