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
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
  }

Link to comment
Share on other sites

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.

Link to comment
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.