Jump to content

URI Routing


ShoeLace1291

Recommended Posts

So I'm trying to make an MVC framework from scratch and need a way to route my uri segments... what I need to do is determine what controllers and methods need to be set based on the given URI.  For example, a uri of forums/xbox-360/1 would be directed to the forums controller and the view forum method.  What would be the most efficient way to go through all of the uri segments to determine if they should be set to controller  directories, controllers, or methods?

Link to comment
Share on other sites

Routers are pretty much standart. Explode the uri, iterate, check for directories, if class exists, if it has that method and so on. Don't worry too much about efficiency because the overhead is generally minimal.

 

Basically, you'll need to go over each uri piece and check first if it's a directory. If it is, append it to the current working directory (a variable that has the base). Next check if a controller file and class exist with the uri piece. Finally, check if that class has the method. Some defaults can be set, for example when no method is set, you can search for the "index" method. Reflection can also be useful to check if a class has a method, if it's public and if any arguments need to be passed (uri parameters can be mapped to method arguments).

 

Writing a full-fledged example in here would be too long. However, you can take a look at a router I've build for my framework. It's simple enough to be easily understood and it has all the bells and whistles.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.