7

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!

Elliot
  • 1,457
  • 13
  • 40
  • Just define static routes first and put this catch all group at the very end. That's perfect approach as the routing order matters. – Chris Cynarski Feb 15 '17 at 01:22
  • Hi Chris, that would be my normal approach, but of course with Spark the static routes that Spark defines are not in your web.php routes file; they are somewhere else. – Elliot Feb 15 '17 at 16:50

3 Answers3

5

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!

davidsneal
  • 138
  • 1
  • 9
2

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.

Elliot
  • 1,457
  • 13
  • 40
0

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.

EddyTheDove
  • 12,979
  • 2
  • 37
  • 45
  • Hi and thanks for that. In Spark, is has some pre-defined routes (e.g. /settings) that are not in the web routes file - the framework calls them after the routes file. So my question is around either using a pattern to exclude those, or knowing how to get the framework to run Spark's routes first. – Elliot Feb 08 '17 at 07:43