Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jlmmns's avatar
Level 12

How to overwrite Spark's routes?

Is there an easy way to overwrite Spark's routes, without modifying the source files?

0 likes
17 replies
zanematthew's avatar

What are you looking to do?

I'm curious as well, I'd like to know if its possible to do something like /terms to /terms-of-service or /register to /join.

Granted we can also just create our own routes, then call the needed view.

jlmmns's avatar
Level 12

@zanematthew Well, to simply use /login in my own routes.php file.

I would like to overwrite the same routes that Spark is already using.

That way, I can translate my (Spark's) routes with a localization package.

I guess I could extend Spark's service provider class, and change the method that adds Spark's routes, but it's just too much of a hassle.

zanematthew's avatar

@EvenStevens @jlmmns you can just add the route to your app/Http/routes.php file. Spark will use whats in your routes.php file first, then use its own.

Route::get('/login', function(){
    // your custom logic here
    dd('hi');
});     

Is that not what your looking for?

EventFellows's avatar

That does not work because the routes in the vendor routes.php file (i.e. vendor\laravel\spark\src\Http\routes.php) take precedence over the app/Http/routes.php file. I believe it has to do with the order these files are loaded (which I did not find yet)

zanematthew's avatar

I just checked, and if it makes a difference it is the first line in my routes.php file.

EventFellows's avatar

@zanematthew That's strange, it definitively does not do it on my setup, not on homestead and also not on the server.

Can you confirm that you have not removed that route from the spark routes.php file? That would be the only thing that comes to my mind for this difference...

Where is the priority of the different route files defined - seems I have to digg deeper here?

jlmmns's avatar
Level 12

@zanematthew Can you try placing your /login route within the 'web' middleware?

Not sure if it matters, but maybe it has to do with that?

zanematthew's avatar

@jlmmns Tested with this:

Route::get('/login', function(){
    dd('hi'); // this is ignored
});

$router->group(['middleware' => ['web']], function ($router) {
    $router->get('/login', 'WelcomeController@show'); // this works
});
jlmmns's avatar
Level 12

@zanematthew Hm, then I guess it has been fixed in the version you're using...

I haven't tested it anymore with a version higher than 1.0.7, I think.

@EventFellows Are you using the latest version of Spark?

EventFellows's avatar

I am using Spark 1.0.13 and it has that problem, too. But strangly enough I double checked with another spark install that I did back in the days to play around - there is works like mentioned by @zanematthew ( same environment, same machine)

So I feel it must have something to do with anything I changed during develpment, though not doing anything (consciously) in the vendor files or route files....

EventFellows's avatar

Ok, got it after quite som trial-and-error.

These two files must be listed in \config\app.php in providers-array in this order to allow overwrites of spark-routes.

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

I installed spark the windows way (like described in official documentation) and there was nothing mentioned about a specific oder of providers being needed.

Hope this also works for you @jlmmns and others with the same issue in future.

7 likes
ejdelmonico's avatar

If I might make a suggestion, if you are going to experiment with routes and who/what has precedence, don't forget to clear the caches with artisan or you may end up chasing your tail.

2 likes

Please or to participate in this conversation.