Campustream 1.0
A social network MQP for WPI
|
00001 <? 00002 00006 class Apiv1_Controller extends Controller { 00007 00008 // this method acts as a subrouter for the api. it assumes that -all- requests will 00009 // come in with two args- controller and method. it checks the $api array in routes 00010 // for valid matches before allowing routing to occur. 00011 public function dispatch($args) { 00012 Hub::$activecontroller = null; 00013 00014 require( $GLOBALS['APPROOT'] . 'application/config/routes.php' ); 00015 00016 $uri = "{$args['controller']}/{$args['method']}"; 00017 00018 // use everything passed into $_GET as a url arg for compatibility. 00019 $args['args'] = $_GET; 00020 00021 // check to see if the route exists in the $api array- if it does, re-run 00022 // the router with that. 00023 foreach( $api as $regex => $call ) { 00024 00025 if ( preg_match('#^'.$regex.'$#u', $uri) ) { 00026 00027 $routed_uri = preg_replace('#^'.$regex.'$#u', $call, $uri); 00028 $args['path'] = null; 00029 00030 $folders = explode('.', $routed_uri); 00031 if (count($folders) > 1) { 00032 $routed_uri = array_pop($folders); 00033 $args['path'] = join('/', $folders ); 00034 } 00035 00036 list( $args['controller'], $args['method'] ) = explode('/', $routed_uri); 00037 00038 include $GLOBALS['APPROOT'] . "application/controllers/{$args['path']}/{$args['controller']}.php"; 00039 00040 $args['controller'] = ucfirst($args['controller']); 00041 00042 $reflector = new ReflectionClass("{$args['controller']}_Controller"); 00043 00044 // Make sure Controller implements REST interface 00045 if ( ! $reflector->implementsInterface('REST') ) { 00046 break; 00047 } 00048 00049 $args['key'] = $_REQUEST['key']; 00050 00051 Router::run($args); 00052 return; 00053 } 00054 } 00055 00056 // let the client know that we acknowledged the request but are refusing to 00057 // respond to it. 404 would not be appropriate here 00058 Hub::http_error(501, "API Method not implemented"); 00059 00060 } 00061 }