Jump to content

Pretty URL's With MVC Framework


ShoeLace1291

Recommended Posts

So, I am beginning the process of coding a PHP application website, which will be coded in the MVC architecture,but would like to implement the method of using Pretty URL's with it.  Since I have never created my own Pretty URL code before, I'm not sure where to start.  In the past, I would use query string URL's(I think  that's the correct term to use) such as example.com/index.php?act=controller&sub=method&id=123456... With this website, I would prefer the URL's to be something more like example.com/controller/method/article-title/123456.  But, like I said, I'm not even sure of where to start.

 

How would I point the URL correctly if...

The method provided isn't found, is invalid, or not provided at all? example.com/controller/article-title/123456  (would  point  to example.com/provided-controller/default-method/page-title/123456

 

The method provided is found, is valid, and is provided, but no controller is found or is invalid or is not provided?  example.com/article-title/123456  (should point to example.com/default-controller/provided-method/123456)

 

I hope I am being clear on everything and that you get what I'm talking about.  Basically, I want to do something like the CodeIgniter URL Router system.

 

Thanks for your time!

Link to comment
Share on other sites

To do this well, it's not really something that can be covered within a simple forum reply, and there are also numerous ways of implementing it. The basics however are simple:

 

Break the url into parts, and match those parts against a series of regular expressions. If those parts regular expressions match, check to see if we can turn that match into an object and method within said object. If we can, execute them, otherwise, rinse and repeat.

 

As an example, I'll show you how I have implemented it. It basically all comes down to the order in which your routes are matched. If you look at my framework's (Proem - yes I'm going to use it as an example) default routes (here) you should be able to see how this can be implemented (Each of these *tokens* starting with : are replaced internally with more complex regular expressions). When it comes time to attempt to dispatch a request to a controller, all of these route rules are looped through and checked against the current request url. When one matches, the dispatcher will check to see if it can actually instantiate a controller and call the *action* method.

 

In Proem this happens in two stages. Firstly we loop through all matching routes (here) and when a match is found a route.match event is triggered which in turn tests to see if the object can be dispatched (here). The actual check happens within the standard dispatcher here.

Link to comment
Share on other sites

Thank you, thorpe, for making such an informative post.  I've been snooping around google for a good URI Routing class, but none really seem to do what I want.  I'll just have to study some of your framework as an example and hope that I can come even close to compiling a code that's as versatile.

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.