laravel-route-attributes icon indicating copy to clipboard operation
laravel-route-attributes copied to clipboard

Routes with domains are serving the wrong controller

Open amiranagram opened this issue 3 years ago • 4 comments

Issue:

Because Laravel processes routes on a first-come-first-serve basis, if you have routes with the same URI on different domains, it doesn't matter which domain you are trying to request, Laravel with serve you the first route that matches that URI.

Demo:

With this route definition if you're trying to request admin.localhost/test Laravel will serve you localhost/test.

Route::get('/test', function () {
    //
});

Route::group(['domain' => 'admin.localhost'], function () {
    Route::get('/test', function () {
        //
    });
});

That's why we will have to define the subdomain group first.

With this route definition, Laravel will serve correct routes.

Route::group(['domain' => 'admin.localhost'], function () {
    Route::get('/test', function () {
        //
    });
});

Route::get('/test', function () {
    //
});

Conclusion:

Back to why I'm posting this issue... I know this is not an issue with this package per se, but using it, we are losing the ability to control the order of defined routes.

We could solve this by sorting the collected routes by ones that have a domain defined first, and then registering them.

Currently, I'm unable to fully utilize this great tool, because I have subdomains in my project, but if I find some spare time I'll try to PR a "fix".

Regards.

amiranagram avatar Mar 25 '22 11:03 amiranagram

The problem is not domain related it's the registration route order that causes the issue In a classic route file you will switch routes from explicit to generic to resolve that kind of problem But with this package, it's not possible cause it register as parsing file. I'm working on a fork to add priority to route attribute, hope I can help you soon!

mmoutih avatar Mar 31 '22 15:03 mmoutih

@mmoutih Where are we on route attribute priority, how can I help?

Parables avatar May 10 '22 12:05 Parables

Hi @Parables i had to put the project to standby for a few weeks cause i had some issus with an other project, i thing i will be releasing something by the end of the month.

mmoutih avatar May 10 '22 20:05 mmoutih

The order of the methods is already preserved when defining routes, the order of classes is also preserved alphabetically, seeing how you declare your routes would be nice to get some thoughts on the issue

It would be possible to add the domain routes in a different folder and put this folder in your configuration before the other controller folder

Tofandel avatar Aug 09 '22 12:08 Tofandel

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

spatie-bot avatar Dec 12 '22 11:12 spatie-bot