如何在 laravel 路由中创建排除某些 slug 的 slug 路由?
How to create a slug route excluding some slug in laravel routing?
我目前正在跟踪路线。
$router->get('/contact-us','HomeController@contactUs')->name('contact-us');
$router->get('/about','HomeController@about')->name('about');
现在,我想让通用页面可从以下路线访问,
$router->get('/{slug}','SomeController@about')->name('general-page');
但主要问题是联系我们,关于与 slug 路由匹配的页面和调用错误的控制器。有什么方法可以从一般页面路由中排除此类 slug。
您可以在路线中添加一个模式,其中排除了术语 contact-us
和 about
,如下所示:
$router->get('/{slug}','SomeController@about')
->where('slug', '^((?!about|contact-us).)*$')
->name('general-page');
有关正则表达式的解释,请参阅 here
这样,路由定义的顺序就没有影响了。
我目前正在跟踪路线。
$router->get('/contact-us','HomeController@contactUs')->name('contact-us');
$router->get('/about','HomeController@about')->name('about');
现在,我想让通用页面可从以下路线访问,
$router->get('/{slug}','SomeController@about')->name('general-page');
但主要问题是联系我们,关于与 slug 路由匹配的页面和调用错误的控制器。有什么方法可以从一般页面路由中排除此类 slug。
您可以在路线中添加一个模式,其中排除了术语 contact-us
和 about
,如下所示:
$router->get('/{slug}','SomeController@about')
->where('slug', '^((?!about|contact-us).)*$')
->name('general-page');
有关正则表达式的解释,请参阅 here
这样,路由定义的顺序就没有影响了。