How to exclude bullets from a Laravel route pattern

I have a Laravel Spark application and I would like to use the first two parameters in the route for the team and the project, with exceptions like about_us, settings, api, etc.

I set up my routes similar to:

Route::pattern('team', '[a-zA-Z0-9-]+'); Route::pattern('project', '[a-zA-Z0-9-]+'); Route::get('/home', ' HomeController@show '); Route::group(['prefix' => '{team}'], function () { Route::get('/', ' TeamController@dashboard '); Route::group(['prefix' => '{project}'], function () { Route::get('/', ' ProjectController@dashboard '); ... //Spark defines routes such as /settings after the apps routing file is processed; //thus I cannot route to /settings as it caught by /{team}. 

I'm struggling to do one of two things. Or, excluding values ​​such as "api", "settings", etc. From the template {team}; or run Laravel Spark routes in front of my web routes so I can make sure all valid routes are checked before all / {command}.

Any ideas would be appreciated!

+7
laravel laravel-routing laravel-spark
source share
3 answers

I seem to have solved it using the following pattern:

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

For those who are not familiar with this issue, the principles are as follows. In a simple Laravel installation, you can re-order your routes to make sure they are processed in the correct order by adding wildcards after your fixed routes.

With Spark, Spark has a number of routes. Preferring not to interfere with this, making it easier to update Spark later, you can use the route pattern to limit the valid values ​​for your parameter. As such, with some Googling on RegExs, I seem to have found a pattern that excludes slugs that map to my {team} parameter.

I find that adding extra exceptions is as simple as inserting a pipe operator.

This also obviously works on standard Laravel installations, but reordering your routes is probably the best first call.

+2
source share

One suggestion I would have is to have the teams prefix, and then the team name after that, you may find that you want to add more catch-alls types, like this for another section, and run into big problems along the line. Perhaps listing all teams using the index of this closure might be useful for system administrators?

If you want to continue working along this route, take a look at config.app.php , I believe that switching to the next two providers may well meet your expectations. Final result:

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

I use the latest version of Spark after the most recent installation, it seems, now by default, apologize if this is a red herring!

+5
source share

First you need to determine the routes that you want to exclude. Then define your templates below them. They will take precedence over patterns, because Laravel routes are evaluated in upper and lower order.

0
source share

All Articles