Minimal PHP Controller URL dispatch


/ Published in: PHP
Save to your folder(s)

Very minimal. Some homegrown API seeds from a while ago. Just posting for use as some php examples.


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3.  
  4. Requires an .htaccess file that rewrites the request to q=, like,
  5.  RewriteCond %{REQUEST_FILENAME} !-f
  6.  RewriteCond %{REQUEST_FILENAME} !-d
  7.  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
  8.  
  9. Now tha I look at it, not sure why I just didn't explode $_REQUEST_URI.
  10.  
  11. */
  12.  
  13. $debug = 1;
  14. $q = explode('/', $_REQUEST['q']);
  15. $module = $q[0];
  16. $op = $q[1];
  17. $debug ? '<pre>'.print_r($q).'</pre>' : '';
  18. $source = 'http://finance.yahoo.com/d/quotes.csv?s=';
  19. $nyse_source = 'http://www.csidata.com/factsheets/nyse.txt';
  20. $nyse_index = 'nyse_index.txt';
  21. $nyse_index_path = $files.$nyse_index;
  22. $files_dir = './files/';
  23. $include_dir = './include/';
  24. $module_path = $include_dir.$module.'.inc';
  25.  
  26. switch ($module) {
  27.  
  28. case 'fields':
  29. include $module_path;
  30. //$csvdata = get data for $q[1];
  31. break;
  32.  
  33. case 'nyse':
  34. include $module_path;
  35.  
  36. if ($op == 'refresh') {
  37. $new_index = nyse_fetch();
  38. $ret = nyse_compare($new_index);
  39. print $ret;
  40. }
  41. elseif ($op == 'load') {
  42. $ret = nyse_load_db();
  43. print $ret;
  44. }
  45. else {
  46. // for now just redirect
  47. header('Location: http://www.xyz.com/api/files/'.$nyse_index);
  48. // in the future print header and read text file
  49. //nyse_send_index();
  50. }
  51.  
  52. break;
  53.  
  54. } //close switch
  55.  
  56.  
  57. echo 'hello';
  58.  
  59. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.