开发者

Dynamic Routing with an MVC Foundation - i18n and l10n

开发者 https://www.devze.com 2022-12-15 12:10 出处:网络
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 wa

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 开发者_StackOverflowdatabase 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:

  • No application logic exposed
  • Translatable URLs (i18n)
  • Dynamic and ability to add, remove, edit urls or pages without touching application code

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.


I believe (although I'm not sure) Akelos supports Translatable URLs.


Since you seem to be rolling your own MVC, what's preventing you from just doing what you want? In fact, I do that myself in my own little MVP framework.

I just use the first segment after the domain to load the equally named controller (presenter), and then I pass it the remaining segments and the query string as arrays. It's then up to each controller what to do with those. Minus the i18n stuff - I handle that semi-globally.

0

精彩评论

暂无评论...
验证码 换一张
取 消