4

I have a few routes that takes a couple of UUIDs as parameters:

Route::get('/foo/{uuid1}/{uuid2}', 'Controller@action');

I want to be able to verify that those parameters are the correct format before passing control off to the action:

Route::pattern('uuid1', '^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$');

This works fine. However, I really don't want to repeat that pattern so many times (in the real case I have it repeated 8 times for 8 different UUID route parameters).

I can't do this:

Route::get('/foo/{uuid}/{uuid}', 'Controller@action');

Because that produces an error:

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

I can lump them all into a single function call since I discovered Route::patterns:

Route::patterns([
    'uuid1' => '^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$',
    'uuid2' => '^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$',
]);

But that is still repetitious. Is there a way I can bind multiple pattern keys to a single regular expression?

Ideally I'd like to find a way that avoids something like this:

$pattern = 'uuid regex';
Route::patterns([
    'uuid1' => $pattern,
    'uuid2' => $pattern,
]);

1 Answer 1

6

There's no built in way to handle this, and I actually think the solution you found is pretty nice. Maybe a bit more elegant would be this:

Route::patterns(array_fill_keys(['uuid1', 'uuid2'], '/uuid regex/'));
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the response. I was really hoping there was a super-secret obscure yet elegant function built just for this purpose that I just could not find.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.