Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: Mojolicious url rewritingby haukex (Archbishop) |
on Apr 20, 2021 at 04:09 UTC ( [id://11131499]=note: print w/replies, xml ) | Need Help?? |
the URL '/user/1' should dynamically point and rewrite to '/user/John-H' in the browser address bar By this I'm assuming you mean redirect? and in link mouseover on the page This is a little bit unclear: if you're generating the pages yourself, then it would be easiest to just insert the correct link in the first place, like I did in the code below. Otherwise, if your HTML really contains a link like <a href="/user/1"> and you want it to appear differently on mouseover, you'll have to solve that with JavaScript. I have mapped id to value in the 'users' cache. This seems a bit strange: a Mojo::Cache, being a cache, is transient, so I assume you're not actually using that as your data store in your actual application (and if you are, you shouldn't). You may have oversimplified your question a bit, since it's unclear what data source you're getting user IDs and names from, like a database? Anyway, I've just used a dummy. restrict the value of id to be an even number and less than 10. That's done simply enough with a regex of [02468], but since I suspect in your actual application you probably don't want to limit it to numbers below 10, in the following code I just limited it to any even integer. Also note that your description is not self-consistent. You want /user/1 to rewrite to /user/John-H, but you also want to restrict IDs to even numbers. It's also unclear if/how you want to map 'John H' to 'John-H' and back, and how you want to handle '/user/NonexistentUser'. Anyway, here's my best guess:
Tests:
Minor typo fixes in text.
In Section
Seekers of Perl Wisdom
|
|