Elm url routing advice


#1

Hi, I have the basics down with elm, but now that I am making a web app I got stuck at the routing. I can do basic url routing, but what I want is the Home Route at the top, with a login prompt and once the user has logged in the url must become /username/. In other words, say my app lives at localhost:8000/, that url must route to Home, which I can do already, but how can I make localhost:8000/annie route to the User Route? “annie” is only one user, there can be many such urls, for instance localhost:8000/bob or localhost:8000/peter. How can this be done or is there a better alternative?

Thanks in advance.


#2

Use oneOf

route : Parser (Route -> a) a
route =
  oneOf
    [ map Home top
    , map Admin (s "admin") 
    , map User string 
    ]

put all the special links (e.g. /admin, /login or whatever you need) above the User one and everything should be fine.


#3

Thank you, but how wil I then be able to route to another url, say Settings Route, after the user like localhost:8000/bob/settings.

Thanks in advance!


#4

You would add another parser in the list, e.g.

map Settings (string </> s "settings")

where | Settings String gets the username as the parameter.


#5

Thank you very much. I was a bit confused, but now I get it.


closed #6

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.