Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude slugs from a Laravel route pattern

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

I have 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's caught by /{team}.

I am struggling to do one of two things. Either, exclude values like 'api', 'settings' etc from the {team} pattern; or get the Laravel Spark routes to run before my web routes, so that I can ensure all valid routes are checked before the catch-all of /{team}.

Any ideas would be appreciated!

like image 986
Kurucu Avatar asked Feb 07 '17 19:02

Kurucu


People also ask

What is prefix in Laravel route?

Path prefixes are used when we want to provide a common URL structure. We can specify the prefix for all the routes defined within the group by using the prefix array option in the route group.

What is reverse routing in Laravel?

Laravel reverse routing is generating URL's based on route declarations. Reverse routing makes your application so much more flexible. For example the below route declaration tells Laravel to execute the action “login” in the users controller when the request's URI is 'login'.


2 Answers

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!

like image 194
davidsneal Avatar answered Nov 14 '22 17:11

davidsneal


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.

like image 28
Kurucu Avatar answered Nov 14 '22 17:11

Kurucu