如何在Laravel 5.5中允许路由参数使用/或-

时间:2018-10-23 06:21:22

标签: laravel laravel-5

有关示例链接,

https://ddskart.com/product/COPIER%20TONER/NP-6/7/8000/71

如何构造GET路线?

路线必须遵循给定的格式。

Route::get('product/{category}/{model}/{product_id}', function ($category, $model, $product_id) {
    // do whatever ...
});

参数可能在的位置

$category   = 'COPIER TONER';
$model   = 'NP-6/6/7/8000'; 
$product_id = 71;

2 个答案:

答案 0 :(得分:0)

使用urlencode()和urldecode()php函数。

答案 1 :(得分:0)

好吧,您可以在路由中使用正则表达式约束。请参阅Laravel Regular Expression Constraints

Route::get('product/{category}/{model}/{product_id}', function ($category, $model, $product_id) {
    // do whatever ...
})->where([
    'category'   => '[\w\s]+',
    'model'      => '([a-zA-Z]+)\-(\d+)\/(\d+)\/(\d+)',
    'product_id' => '[\d]+'
]);

请注意,在上面的示例中,为 category model product_id 参数用于匹配问题中的给定网址。您可以根据自己的逻辑调整它们。关键是您可以在构建复杂路径时利用Laravel正则表达式约束。