I've recently been improving my skills with web programming to follow the saner and more maintainable MVC style of coding. However, one thing which I used to do with my "roll your own" framework was flexible dynamic routing based around mod_rewrite. This appears to be a sore issue with things like cakephp, zend, etc.. and its causing me some headaches by trying to duplicate the functionality which I had.
In a roll your own context, you could do something like the following:
<custom htaccess rules before>
RewriteRule ^([A-Za-z0-9-/]+)$ index.php?q=$1 [NC,L]
which rewrites all matched urls to an arbitrary script that handles the url, parses the combinations of "/abc-123/abc-456/controller-value"
etc to any list of pages or actions, functions etc that can be set up from a database table, hardcoded, bla bla.. The custom rules before can pass through matched requests for files that exist for resources on the server, or setup admin routing etc.
However, once the MVC url policy kicks in, it is extremely hard to override this behaviour with something that hides the application logic from the outside world. I never really understood why anyone would want to expose function names to a user, and this is my real bugbear. It seems unnecessary and an imposition too far, from usability and security perspectives.
So - the question is this, how to approach a rewritten, translatable and friendly url policy while maintaining a solid MVC foundation to the application?
Requisites:
Fun times! :D
Example URLs:
/en/news/story-title
/pt/noticias/titulo
where the urls retrieve localised content based on language string passed.
MVC is just an architectural pattern which has nothing to do with URL structure. Using a custom routing layer will not break MVC by any means. If centralized routing is OK for you, then, for example, you can have a config file which maps URL patterns (regexes or some kind of simplified syntax) to controller actions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With