简体   繁体   English

Laravel 5.2 中一个路由中的多个路由参数模式

[英]Multiple route argument patterns in one route in Laravel 5.2

Hello,你好,

for start, I really tried to google this.首先,我真的试图用谷歌搜索这个。 But it seems impossible to use a route argument pattern more than once in one route.但是似乎不可能在一个路由中多次使用路由参数模式。 My goal is an argument validation in route definition for routes like /resource/{uuid}/subresource/{uuid} without having to check those arguments manually in controller.我的目标是在路由定义中对 /resource/{uuid}/subresource/{uuid} 等路由进行参数验证,而无需在控制器中手动检查这些参数。

Let's assume we have:让我们假设我们有:

$router->pattern('uuid', '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}');

Works perfectly for routes like非常适合像这样的路线

$router->get('/payment/{uuid}', 'Payments@payment');
$router->get('/users/{uuid}', 'Users@get');
//etc..

BUT但是

$router->get('/users/{uuid}/order/{uuid}', 'Controller@someStuff');

throws an error:抛出错误:

"Route pattern "/users/{uuid}/order/{uuid}" cannot reference variable name "uuid" more than once."

Seems legit.似乎是合法的。 But I just want to validate arguments by regex DRY and other approaches like below does not work too:但我只想通过正则表达式 DRY 验证参数,而其他类似下面的方法也不起作用:

$router->get('/users/{userId}/order/{orderId}', 'Controller@someStuff')
        ->where(['userId' => 'uuid', 'orderId' => 'uuid']); 
// or

$router->get('/users/{userId:uuid}/order/{orderId:uuid}', 'Controller@someStuff');

// ..and vice versa

Only thing that works is this:唯一有效的是:

$router->get('/users/{userId}/order/{orderId}', 'Controller@someStuff')
        ->where(['userId' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}', 'orderId' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}']);

... but I really don't wanna go through this way. ……但我真的不想走这条路。 That's actually the only way it worked.这实际上是它工作的唯一方式。

Does anyone knows some trick, how to apply route argument pattern multiple times?有谁知道一些技巧,如何多次应用路由参数模式?

Thanks, any help would be appreciated...谢谢,任何帮助将不胜感激...

Laravel doesn't seem to support named route patterns. Laravel 似乎不支持命名路由模式。 I've had to deal with this sort of thing before and I've found this to be a reliable way of doing things:我以前不得不处理这种事情,我发现这是一种可靠的做事方式:

Open up RouterServiceProvider.php in app/Providers and add the following to your boot() method:app/Providers打开RouterServiceProvider.php并将以下内容添加到您的boot()方法中:

/**
 * Define your route model bindings, pattern filters, etc.
 *
 * @param  \Illuminate\Routing\Router  $router
 * @return void
 */
public function boot(Router $router)
{
    $uuidPattern = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';

    $router->pattern('userId', $uuidPattern);
    $router->pattern('orderId', $uuidPattern);
    $router->pattern('anotherUuid', $uuidPattern); // Just an example - delete this line!

    parent::boot($router);
}

Basically, you can add all your router patterns in there and they will be made available to your routes.基本上,您可以在其中添加所有路由器模式,它们将可用于您的路由。 They're all in one place so it's easy to remember where they are should you need update them and you can reuse the same pattern for multiple parameters.它们都在一个地方,因此如果您需要更新它们,很容易记住它们的位置,并且您可以对多个参数重复使用相同的模式。

In Laravel 7.x在 Laravel 7.x 中

You should define these patterns in the boot method of your RouteServiceProvider您应该在 RouteServiceProvider 的 boot 方法中定义这些模式

public function boot() {
    $uuidPattern = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';

    Route::pattern('userId', $uuidPattern);
    Route::pattern('orderId', $uuidPattern);

    parent::boot();
}

Above method applied to all routes using that parameter name以上方法应用于使用该参数名称的所有路由

Route like,路线喜欢,

Route::get('/users/{userId}/order/{orderId}', function () {
    // Only executed if {userId} and {orderId} accepts given regular expression($uuidPattern)...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM