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

How to use url_for in mounted router? #132

Open
flosse opened this issue Nov 24, 2016 · 9 comments
Open

How to use url_for in mounted router? #132

flosse opened this issue Nov 24, 2016 · 9 comments

Comments

@flosse
Copy link

flosse commented Nov 24, 2016

let's say I mounted a router to admin/ and use url_for within a handler of that router.

let routes = router!(
  foo: post "foo" => |req: &mut Request| {
    Ok(Response::with(Redirect(url_for!(req, "dashboard"))))
});
let admin = Chain::new(router);
let mut mounts = Mount::new();
mounts.mount("/admin", routes);

The redirect url points to /dashboard instead of /admin/dashboard.
How would you solve this?

@Hoverbear
Copy link

This is intended as far as I know:

Src

Mount a handler on a sub-path, hiding the old path from that handler.

You should be able to get the original URL using this

@flosse
Copy link
Author

flosse commented Nov 25, 2016

Exposing the OriginalUrl doesn't help, because url_for takes a Request :-/

@flosse
Copy link
Author

flosse commented Nov 25, 2016

At the moment I can see these solutions:

  • a custom function that clones the request and overrides it's url with the OriginalUrl and calls url_for that reimplements url_for
  • a new router method (e.g. url_for_from_original) that can be used by setting a feature mount
  • hacking url_for that magically (what I dislike) uses OriginalUrl if the feature mount is set

What do you think?

@untitaker
Copy link
Member

@flosse IMO the proper solution is to add a parent: Option<Request> attribute to Iron's core request struct and always use that inside of router.

However, we have to find out how to be able to modify a small part of the request without copying everything over.

@dbrgn
Copy link

dbrgn commented Apr 3, 2017

I was also bitten by this.

@flosse did you find a solution in the meantime (as per the discussion in #133)?

@flosse
Copy link
Author

flosse commented Apr 4, 2017

@flosse did you find a solution in the meantime (as per the discussion in #133)?

no. But to be honest, I switched to Rocket ;-)

@reem
Copy link
Member

reem commented Apr 8, 2017

I think the real long term solution to this problem is to just merge router and mount into a single API that provides both features, so they can cooperate to make these problems go away.

@dbrgn
Copy link

dbrgn commented Apr 9, 2017

Yeah, that sounds useful to me. Routing and mounting are very closely related anyways :)

@dbrgn
Copy link

dbrgn commented Apr 11, 2017

@reem are there already any concrete plans for such a merge?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants