ShoeLace1291 Posted December 27, 2012 Share Posted December 27, 2012 I am trying to write a script that writes url's to the correct "source" file. Basically, I want to write a script that works kind of like CodeIgniter's URI routing class, but not as complex. I want to find what class and what method to use, but instead of classes and methods, I am using just files and functions. For example: forums/threads/some-cool-thread/2051 would route to sources/forums/threads.source.php with a couple of extra segments. another example: members/register would route to sources/members.php with the register function since members is a file within the sources directory and not a directory in itself. For some reason, I am having a bunch of trouble with coming up with the logic for what I want to do. If someone could maybe layout a flow chart or something just to give me an idea, that would be wonderful! Quote Link to comment https://forums.phpfreaks.com/topic/272400-uri-routing-help/ Share on other sites More sharing options...
requinix Posted December 27, 2012 Share Posted December 27, 2012 Question: how is the code supposed to know that forums/threads/some-cool-thread/2051 doesn't map to sources/forums/threads/some-cool-thread/2051.source.php? Quote Link to comment https://forums.phpfreaks.com/topic/272400-uri-routing-help/#findComment-1401497 Share on other sites More sharing options...
trq Posted December 27, 2012 Share Posted December 27, 2012 Routing is one of those things that requires a certain amount of complexity to achieve flexibility. You could easily solve part of your problem by simply directing all traffic to an index.php page containing a switch statement that maps urls to included files: switch (true) { case $_SERVER['REQUEST_URI'] == '[color=#282828][font=helvetica, arial, sans-serif]forums/threads/some-cool-thread':[/font][/color] [color=#282828][font=helvetica, arial, sans-serif] include '[/font][/color][color=#282828][font=helvetica, arial, sans-serif]sources/forums/threads.source.php';[/font][/color] [color=#282828][font=helvetica, arial, sans-serif] [/font][/color][color=#282828][font=helvetica, arial, sans-serif]break;[/font][/color] case $_SERVER['REQUEST_URI'] == '[color=#282828][font=helvetica, arial, sans-serif]members/register[/font][/color][color=#282828][font=helvetica, arial, sans-serif]':[/font][/color] [color=#282828][font=helvetica, arial, sans-serif] include '[/font][/color][color=#282828][font=helvetica, arial, sans-serif]sources/members.php[/font][/color][color=#282828][font=helvetica, arial, sans-serif]';[/font][/color] [color=#282828][font=helvetica, arial, sans-serif] [/font][/color][color=#282828][font=helvetica, arial, sans-serif]break;[/font][/color] } This however doesn't solve your issue with extra paramaters being passed. As soon as you require that, you need to start using regex and thing can start to get more complicated. You will also eventually require more flexibility than you can achieve with the simple example above. If your trying to write your own because you want to learn more about how its done. All well and good, just think the problems through as they arise. Be aware though, to make a decent router, your going to need to make a trade off between complexity and flexibility. If you just want to get a router in place and move on. Why not use an existing routing component? Symfony provides it's routing implementation as a component that does not require the framework itself. Hell, even my framework (Proem) has a decent router which I am currently in the process of separating out into it's own package. Don't reinvent the wheel unless you have/want to. Quote Link to comment https://forums.phpfreaks.com/topic/272400-uri-routing-help/#findComment-1401513 Share on other sites More sharing options...
trq Posted December 27, 2012 Share Posted December 27, 2012 ps: Sorry about the formatting. This forum software sux balls. Quote Link to comment https://forums.phpfreaks.com/topic/272400-uri-routing-help/#findComment-1401514 Share on other sites More sharing options...
Muddy_Funster Posted December 27, 2012 Share Posted December 27, 2012 ps: Sorry about the formatting. This forum software sux balls. Maybe you should take that up with an admin.......oh, wait Quote Link to comment https://forums.phpfreaks.com/topic/272400-uri-routing-help/#findComment-1401516 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.