How to exclude slugs from a Laravel route pattern How to exclude slugs from a Laravel route pattern laravel laravel

How to exclude slugs from a Laravel route pattern


One suggestion I'd have, is to consider having the prefix of teams, then the team name after, you may find you want to add more sort of catch-alls like this for another section and run into more problems down the line. Perhaps listing all the teams using the index of this closure could be of benefit to admins of the system too?

If you'd like to continue down this route, take a look in your config.app.php, I believe that switching around the following two providers may well achieve what you're after. End result order:

App\Providers\SparkServiceProvider::class,App\Providers\RouteServiceProvider::class,

I'm using the latest version of Spark after a recent install myself, this seems to be the default now, apologies if this is a red-herring!


I appear to have solved it, using the following pattern:

Route::pattern('team', '(?!^settings$)([a-zA-Z0-9-]+)');

For those who are new to the question, the principles are as follows. In a plain Laravel installation, you could re-order your routes to ensure they are processed in the right order, putting wildcards after your fixed routes.

With Spark, there are a number of routes all encapsulated away in the Spark package. Preferring not to mess around with this, allowing for easier Spark upgrades later amongst other things, it is possible to use a route pattern to limit the acceptable values for your parameter. As such, with some Googling on RegExs, I appear to have found a pattern that will exclude slugs matched by my {team} parameter.

I believe that adding more exclusions is as easy as inserting a pipe operator.

This would also obviously work on standard Laravel installations, but re-ordering your routes is probably a better first call.


You should define the routes you want to exclude first. Then define your patterns below them. They will have precedence over the patterns because in Laravel routes are evaluated in top to bottom order.