ShoeLace1291 Posted December 28, 2012 Share Posted December 28, 2012 (edited) 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 December 28, 2012 by ShoeLace1291 Quote Link to comment https://forums.phpfreaks.com/topic/272435-uri-routing-help/ Share on other sites More sharing options...
Muddy_Funster Posted December 28, 2012 Share Posted December 28, 2012 wasn't this exact question posted yesterday? Quote Link to comment https://forums.phpfreaks.com/topic/272435-uri-routing-help/#findComment-1401720 Share on other sites More sharing options...
cpd Posted December 28, 2012 Share Posted December 28, 2012 (edited) 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 December 28, 2012 by CPD Quote Link to comment https://forums.phpfreaks.com/topic/272435-uri-routing-help/#findComment-1401797 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.