Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Routing with an MVC Foundation - i18n and l10n

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:

  • 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.

like image 614
dmp Avatar asked Nov 05 '22 18:11

dmp


1 Answers

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.

like image 79
Ignas R Avatar answered Nov 12 '22 11:11

Ignas R