Jump to content

Help with routing script


Perad

Recommended Posts

Hey,

 

I have created an 'awesome' - as in it works! - routing library.

 

However it is somewhat limited and I don't have the knowledge to take it to the next level.

 

It works something like this. Please note this isn't the actual code.

 

  $segments = explode('/', $url);
  // Trim and reorganise array
  
  if (count($segments) == 0)
  {
    // Use default route
  }

  if (count($segments) == 1)
  {
    // Load module/segment0.php - start class segment0 - load the index function or return 404 if this does not exist
  }

  if (count($segments) == 2)
  {
    // Same as above with the exception that segment1 is the function instead of index
  }

 

This is where I am. I have tested it a lot. Either returns correct function/class or a 404 error.

 

What I want to do is allow for more than 2 segments. I would like segments 3+ to become function parameters.

 

How do I do this?

Link to comment
https://forums.phpfreaks.com/topic/176560-help-with-routing-script/
Share on other sites

I don't entirely understand what you're asking. But you can optimize the script a little by doing this...

  $segments = explode('/', $url);
  // Trim and reorganise array
  
  $count = count($segments); 

  if ($count == 0)
  {
    // Use default route
  }

  if ($count == 1)
  {
    // Load module/segment0.php - start class segment0 - load the index function or return 404 if this does not exist
  }

  if ($count == 2)
  {
    // Same as above with the exception that segment1 is the function instead of index
  }

When it comes to optimizing, since $count can only ever have one value you might aswell...

 

 $count = count($segments); 

  if ($count == 0)
  {
    // Use default route
  }
  elseif ($count == 1)
  {
    // Load module/segment0.php - start class segment0 - load the index function or return 404 if this does not exist
  }
  elseif ($count == 2)
  {
    // Same as above with the exception that segment1 is the function instead of index
  }

 

But it's not answering the original question and like MasterACE14 I don't have a clue what your asking.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.