Posted By

jzumbrun on 02/14/10


Tagged

textmate


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

jatap
vkolev
educsis


CodeIgniter Route Insert


 / Published in: PHP
 

  1. function SmartPageRoutes($action, $slug=NULL, $filename=NULL, $page=NULL)
  2. {
  3. if($slug!=NULL) $slug = $this->_generateSlug($slug);
  4.  
  5. // Add link to routes file
  6. $routeFile = '../primary_app/config/routes.php';
  7. $arr = file($routeFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  8. foreach ($arr as $key => $line)
  9. {
  10. if($action == 'add')
  11. {
  12. // Match all the public functions
  13. if(preg_match('/DONOT ALTER THIS COMMENT... PLACE PAGE ROUTES BELOW/', $line))
  14. $arr[$key] = $line."n".'$route[''.$slug.''] = "'.$filename.'";'."n";
  15. if(preg_match('/DONOT ALTER THIS COMMENT... PLACE CLASS ROUTES BELOW/', $line))
  16. $arr[$key] = $line."n".'$route[''.$slug.'/(.*)'] = "'.$filename.'/$1";'."n";
  17. }
  18. else if($action == 'update')
  19. {
  20. // Match the previous route
  21. $urlPage = '$route[''.$page->linkSlug.'(.*)']';
  22. $urlClass = '$route[''.$page->linkSlug.'/(.*)(.*)']';
  23. if(preg_match("~$urlPage~", $line, $child))
  24. {
  25. $arr[$key] = "n".'$route[''.$slug.$child[1].''] = "'.$filename.'";'."n";
  26. }
  27. if(preg_match("~$urlClass~", $line))
  28. {
  29. $arr[$key] = "n".'$route[''.$slug.$child[1].''] = "'.$filename.'/$1";'."n";
  30. $updateMatch = 1;
  31. }
  32.  
  33. }
  34. else if($action == 'delete')
  35. {
  36. // Match the previous route
  37. $urlPage = '= "smart_pages/'.$page->pageSmartPageFileName;
  38.  
  39. if(preg_match("~$urlPage~", $line))
  40. {
  41. $arr[$key] = "";
  42. }
  43. }
  44.  
  45. }
  46.  
  47. $routes = implode("n", $arr);
  48.  
  49. // Let's make sure the file exists and is writable first.
  50. if (is_writable($routeFile))
  51. {
  52. // In our example we're opening $filename in append mode.
  53. // The file pointer is at the bottom of the file hence
  54. // that's where $somecontent will go when we fwrite() it.
  55. if (!$handle = fopen($routeFile, 'w'))
  56. {
  57. echo "Cannot open file ($routeFile)";
  58. }
  59.  
  60. // Write $somecontent to our opened file.
  61. if (fwrite($handle, $routes) === FALSE)
  62. {
  63. echo "Cannot write to file ($routeFile)";
  64. }
  65.  
  66. fclose($handle);
  67. return true;
  68. }
  69. else return false;
  70. }

Report this snippet  

You need to login to post a comment.