0

Based on this problem, I want to replicate it but my problem is that the array option has more than 3000 posibilities. If I do this:

Route::get('/example/{example}', 'ExampleController')
->where('example','(example-1|example-2|...|example-3999|example-4000)');

I get this error:

preg_match(): Compilation failed: regular expression is too large at offset 72980

Is there a way to use in_array() or similar in the where condition of route? With a custom where condition...

A lot of thanks!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Alvaro_SE
  • 125
  • 2
  • 13

1 Answers1

0

My temporal solution is using a custom middleware and apply only to the routes that use this validation. I don't know if there are a better solution.

class CustomMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        if(!in_array($request->example, $arrayList)) abort(404);
        return $next($request);
    }
}

Route:

Route::get('/example/{example}', 'ExampleController')->middleware('custom');
Alvaro_SE
  • 125
  • 2
  • 13
  • There is a catch to this solution if your actual regex matches slashes. E.g. if you had `Route::get('/example/{example}', 'ExampleController') ->where('example','(example/1|example/2|...|example/3999|example/4000)');` this wouldn't work because Laravel would split `example` and the number so you should be aware of this limitation. – apokryfos Aug 09 '22 at 12:56