ShoeLace1291 Posted July 21, 2012 Share Posted July 21, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/266034-uri-routing/ Share on other sites More sharing options...
Fadion Posted July 21, 2012 Share Posted July 21, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266034-uri-routing/#findComment-1363257 Share on other sites More sharing options...
xyph Posted July 21, 2012 Share Posted July 21, 2012 Use mod_rewrite, rather than try to manage something like this using a folder structure. Dealing with get variables is so much easier Quote Link to comment https://forums.phpfreaks.com/topic/266034-uri-routing/#findComment-1363298 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.