Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

route:after should not be called on $route→next() #2418

Closed
distantnative opened this issue Feb 2, 2020 · 4 comments
Closed

route:after should not be called on $route→next() #2418

distantnative opened this issue Feb 2, 2020 · 4 comments
Assignees
Labels
type: bug 🐛 Is a bug; fixes a bug
Milestone

Comments

@distantnative
Copy link
Member

I just encountered the problem while debugging Retour (distantnative/retour-for-kirby#138) that the route:after hook is a bit problematic in multilang setups.

I would argue the most common use case to hook into a successful routing.
However, the hook is actually called on every route lookup - also if the route does $this→next(), which seems counter intuitive as the first route explicitly says "not me" by calling $this→next()
https://github.com/getkirby/kirby/blob/master/src/Http/Router.php#L115-L117

My suggestion would be to move the hook call out of the loop and right before the return. To really only call the hook on routes that do not skip to the next route.
What do you think @bastianallgeier and @lukasbestle?

@distantnative distantnative added needs: discussion 🗣 Requires further discussion to proceed type: bug 🐛 Is a bug; fixes a bug difficulty: easy 🍓 labels Feb 2, 2020
@lukasbestle
Copy link
Member

I agree that this is a bug in the current implementation.

The issue is that the before hook is called on every route and there is no way to only call it on the route that will be used in the end. So if we do it like you suggest, there will be an asymmetry between the before hook (called for every route) and the after hook (called only on the successful route).

I see two solutions to keep this consistent:

  • We continue to call the after hook on every route, but pass another boolean argument that tells the hook whether the route was used or not.
  • We also only run the before hook once and don't pass the $route variable to it (breaking-change!).

@distantnative
Copy link
Member Author

I like your first suggestion the most.

@bastianallgeier
Copy link
Member

I like the first suggestion as well. It also means we can implement it without a breaking change!

@bastianallgeier
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐛 Is a bug; fixes a bug
Projects
None yet
Development

No branches or pull requests

3 participants