W3docs

Laravel - Using (:any?) wildcard for ALL routes?

In Laravel, you can use the (:any?) wildcard to match any URI for a specific route.

In Laravel, you can use the {any?} route parameter to match any URI segment for a specific route. This parameter can be used in place of a specific segment of the URI, allowing any value to be passed to the route handler. For example, the following route definition would match any URI that starts with test:

Example of using {any?} route parameter in Laravel

Route::get('test/{any?}', function ($value = null) {
    return $value;
});

This would match any URI that starts with test/, and the value passed to the function would be the remaining portion of the URI. For example, visiting test/abc would pass abc to the function, and visiting test/123 would pass 123 to the function.

It's worth noting that if you want to match any HTTP method for a catch-all route, you can use Route::match() instead of the deprecated Route::any(). For example:

Route::match(['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'], '{any?}', function () {
    // Handle any request
});

Alternatively, for a true catch-all route that handles any URI regardless of the method, Laravel provides Route::fallback().