Getting error 500 with a dynamic route using Laravel 8

Viewed 108

I'm getting error 500 when I try visit a dynamic route. The parameter is optional and is working well when I avoid it.

This is the code

Route::get('/{slug?}', function ($request = '') {
    $code = empty($request)? '47ZH2' : $request;
    return view('home', ['code' => $code]);
})->where('slug','^([0-9A-Za-z\-]+)?');

Even I tried without regex but is not working anyway.

1 Answers

/{slug?} would match all routes since it means match single / or any URI after it like /products, /categories etc. These type of routes aren't recommended as they can create unexpected behaviors in the system and long hours of debugging.


If you still wish to keep it, put this route at the end of all routes as it will act as a fallback route if none of the above routes match.

Related