Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/272400-uri-routing-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/272400-uri-routing-help/#findComment-1401513
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.