开发者

Thoughts about Subcategory Routing

I'm building a basic routing system in order to get rid of the index.php?page_id=2 stuff and use nicely formed URL like: news/regional/sports/baseball/junior-league/2010 which, for example, would take me to the baseball news of the junior league in 2010

The basic structure is like this: there is a router that evaluates the url and accesses an appropriate controller and calls a method with some (optional) arguments. The controller then calls the appropriate model class and binds it to a view which is then outputted.

Taking the previous example, I'd like to access: news/regional/sports/baseball/junior-league/2010 which I'd like to call the BaseballController::view(junior-league, 2010) method.

I'd like to keep the approach modular, so without defining (site-)specific routes. I'd just like to have a pattern in my URLs so that I can call the method of the right controller with the right arguments.

How can I format my URLs and specify some evaluation rules, so that this behavior is obtained? (Is this possible?)

Solutions I've thought of:

  • Example url: news/regional/sports/baseball/view/junior-league/2010
    1. explode on the slashes (resulting in an array)
    2. look for an array[0] control开发者_如何学Cler with a method array[1]
    3. If that method exists, then call it with the arguments
    4. If it doesn't, look for an array[1] controller with an array[2] method
    5. etc

      Advantage is that this approach is very modular. Only thing is that the time to access a page gets longer depending on the number of subcategories there are (or is this neglectable?). Maybe the process can be sped up by storing a site map (but thats just what I don't want)

  • Format a url like this: [path to controller]/[controller]/[action]-[argument1]-[argument2] Example: news/regional/sports/baseball/view-junior_league-2010 using this algorithm
    1. strip at the "-" (which generates an array) [news/regional/sports/baseball/view, junior_league, 2010]
    2. take the first elem and explode on the slashes [ [news, regional, sports, baseball, view], junior_league, 2010]
    3. search for a method named array[0][last] (view) in the controller array[0][last-1] (baseball) which is in the directory controllers/news/regional/sports. Use the rest of the array as arguments.

      Disadvantage is that you also use the underscore and the url is not very nice anymore. And maybe it is little time-consuming?

If you've read through all of this, thanks! Maybe you can share your thoughts? Gr Bastiaan


I've been using ToroPHP a lot recently, which uses a PHP array to define a regular expression pattern to match, and a handler to call if there's a match.

Take a look at ToroPHP and maybe extrapolate part of its solution to your needs.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜