Guest User

Untitled

a guest
Jan 17th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. /dev/orders/create
  2.  
  3. <?php
  4. //bootstrap.php code:
  5. Configure::write('Routing.prefixes', array('admin', 'dev'));
  6.  
  7. // AppController::beforeFilter() code:
  8. if (preg_match('/^dev_(.*)$/', $this->params['action'], $subs)){
  9. $new_action = $subs[1];
  10. if (method_exists($this, $new_action)){
  11. $this->params['action'] = $new_action;
  12. }
  13. }
  14. ?>
  15.  
  16. #webroot/.htaccess
  17. RewriteRule ^dev(.*)$ index.php?dev=true&%{QUERY_STRING}
  18. #... rewrite conds ...
  19. RewriteRule ^(.*)$ index.php [QSA,L] #original Cake rewrite
  20.  
  21. // need to override invokeAction
  22. public function invokeAction(CakeRequest $request) {
  23. if ($request->prefix == 'dev') {
  24. if (!method_exists($request->action)) {
  25. $request->action = str_replace($request->prefix.'_', '', $request->action);
  26. $request->prefix = null;
  27. }
  28. }
  29. return parent::invokeAction($request);
  30. }
  31.  
  32. RewriteEngine On
  33. RewriteRule ^dev/(.*)$ $1 //my hack
  34. // /dev/orders/create => /orders/create
  35. // /dev/css/main.css => /css/main.css
  36. // This only changes the url for the next rule
  37. // Cake would have still parsed /dev/orders/create
  38. // but this is needed for the sake of static files in app/webroot
  39. //cake native rewrite
  40. RewriteCond %{REQUEST_FILENAME} !-d
  41. RewriteCond %{REQUEST_FILENAME} !-f
  42. RewriteRule ^(.*)$ index.php [QSA,L]
  43.  
  44. $url = (!empty($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : '';
  45. //$url is still /dev/orders/create, despite the rewrites
  46.  
  47. Configure::write('App.dev', false);
  48. //we use this to check if we're in "dev" mode
  49. //anywhere in the app, just call:
  50. // if ( Configure::read('App.dev') ) ...
  51.  
  52. //check if URL starts with /dev
  53. if (preg_match('|^/dev|', $url)){
  54. /* the most important thing is to trick CakePHP
  55. into thinking it's on a /dev/ subfolder on the server
  56. this preserves all the routing as it
  57. should be, not making it prefixed route */
  58. Configure::write('App.base', '/dev');
  59. Configure::write('App.dev', true);
  60. //this changes /dev/orders/create to /orders/create
  61. //for Cake's Route parser
  62. $_SERVER['REQUEST_URI'] = preg_replace('|/dev|', '', $_SERVER['REQUEST_URI']);
  63. }
  64. ?>
  65.  
  66. <?php
  67. //file: AppController.php
  68. public function invokeAction(CakeRequest $request) {
  69.  
  70. if (Configure::read('App.dev')) {
  71. $new_action = 'dev'.$request->params['action'];
  72. if (method_exists($this, $new_action)){
  73. $request->params['action'] = $new_action;
  74. }
  75. }
  76.  
  77.  
  78. return parent::invokeAction($request);
  79. }
  80. ?>
  81.  
  82. <?php
  83. //OrdersController.php
  84.  
  85. //gets called on /admin/orders/view
  86. public function admin_view(){}
  87.  
  88. //gets called on /dev/admin/orders/view
  89. public function devadmin_view(){
  90. //views have to be defined manually for DEV actions
  91. $this->render('devadmin_view');
  92. }
  93.  
  94. //gets called on /admin/orders/add
  95. // AND /dev/admin/orders/add
  96. public function admin_add() {}
  97.  
  98. ?>
Add Comment
Please, Sign In to add comment