Jump to content

Uri Routing Help


ShoeLace1291

Recommended Posts

So, I am trying to write a script that routes URI's to the appropriate file. I am not using classes, only files(source) and functions(action). I've had a bunch of trouble with getting this to work, and I can't seem to figure out the problem. Basically, I want to route the first segment to a source file if it is a valid source. If a second segment exists and is a valid action(function), then that function will be loaded. So say I have a source file called members.source.php and there is a function called register in it. A user will have to access the uri http://localhost/mya...embers/register to access that page. or myapp/members would be directed to the "_default" action(function). myapp/members/SomeCoolMember would redirect to members.source.php with the viewprofile action(function).

 

I hope you understand what I am trying to get at here. For whatever reason, the variable "source" is not being defined. Here's what I have as my index.php file.

 

$uri_segments = $_SERVER['REQUEST_URI'];

$uri_segments = str_replace('/Base%20Command/', '', $uri_segments);

if(strlen($uri_segments) == 0){

$uri_segments = array();

} else {

$uri_segments = explode('/', $uri_segments);

}

if(count($uri_segments) > 0){

foreach($uri_segments as $key => $segment){

if(!preg_match('^[a-z0-9_-]^', $segment)){

die('Could not accept provied URI string... the string contains invalid characters.<br>'.$segment);

} else {

if(validSource($segment) && empty($source)){

$source = $segment;

} else if(validDirectory($segment) && empty($source)){

$source = $segment;

} else if(isset($source) && empty($action)){

if(validFunction($segment)){

$action = $segment;

} else {

die('404 Error: Page not found. 001');

}

}

}

}

} else {

$source = 'index';
$action = '_default';

}

function validSource($sourceName){

   if(file_exists(DOC_ROOT.'/sources/'.$sourceName)){

       return TRUE;

   } else {

       return FALSE;

   }

}

function validDirectory($dirName){

   if(is_dir(DOC_ROOT.'/sources/'.$dirName)){

       return TRUE;

   } else {

       return FALSE;

   }

}

function validFunction($fnc){

   if(function_exists($fnc)){

       return TRUE;

   } else {

       return FALSE;

   }

}

Edited by ShoeLace1291
Link to comment
Share on other sites

Your control flow is up-the-creek. You need to review your if statements because your final else if tests if $source has been set BUT its included in the control flow that sets the $source variable therefore, if $source gets set it will never make it to the final else if, it'll skip over it.

Edited by CPD
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.