ShoeLace1291 Posted June 18, 2012 Share Posted June 18, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/264365-pretty-urls-with-mvc-framework/ Share on other sites More sharing options...
MaaSTaaR Posted June 18, 2012 Share Posted June 18, 2012 Use mod_rewrite to do that There are a lot of tutorials that explain mod_rewrite, just use Google. Quote Link to comment https://forums.phpfreaks.com/topic/264365-pretty-urls-with-mvc-framework/#findComment-1354801 Share on other sites More sharing options...
trq Posted June 18, 2012 Share Posted June 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264365-pretty-urls-with-mvc-framework/#findComment-1354803 Share on other sites More sharing options...
ShoeLace1291 Posted June 18, 2012 Author Share Posted June 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264365-pretty-urls-with-mvc-framework/#findComment-1354825 Share on other sites More sharing options...
trq Posted June 18, 2012 Share Posted June 18, 2012 Someone just posted a link to this tutorial in my twitter feed. You might find it useful. Quote Link to comment https://forums.phpfreaks.com/topic/264365-pretty-urls-with-mvc-framework/#findComment-1354832 Share on other sites More sharing options...
ignace Posted June 18, 2012 Share Posted June 18, 2012 If you don't have access to PHP 5.4 you can also try: https://github.com/robap/php-router If you are not satisfied by that then search for "php router" you'll find a few on google. Quote Link to comment https://forums.phpfreaks.com/topic/264365-pretty-urls-with-mvc-framework/#findComment-1354903 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.