MyController.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. App::uses('Controller', 'Controller');
  3. /**
  4. * DRY Controller stuff
  5. * 2011-02-01 ms
  6. */
  7. class MyController extends Controller {
  8. /**
  9. * Fix for asset compress to not run into fatal error loops
  10. * 2012-12-25 ms
  11. */
  12. public function __construct($request = null, $response = null) {
  13. parent::__construct($request, $response);
  14. if (strpos($this->here, '/js/cjs/') === 0 || strpos($this->here, '/css/ccss/') === 0) {
  15. unset($this->request->params['ext']);
  16. }
  17. }
  18. /**
  19. * Add headers for IE8 etc to fix caching issues in those stupid browsers
  20. * 2012-12-25 ms
  21. */
  22. public function disableCache() {
  23. $this->response->header(array(
  24. 'Pragma' => 'no-cache',
  25. ));
  26. return parent::disableCache();
  27. }
  28. /**
  29. * Fix encoding issues on Apache with mod_rewrite
  30. * Uses Configure::read('App.additionalEncoding') to additionally escape
  31. *
  32. * Tip: Set it to `1` for normal mod_rewrite websites routing directly into webroot
  33. * If you use another setup (like localhost/app/webroot) where you use multiple htaccess files or rewrite
  34. * rules you need to raise it accordingly.
  35. *
  36. * @param string|array $url A string or array-based URL
  37. * @param integer $status Optional HTTP status code (eg: 404)
  38. * @param boolean $exit If true, exit() will be called after the redirect
  39. * @return void
  40. * 2013-03-02 ms
  41. */
  42. public function redirect($url, $status = null, $exit = true) {
  43. $run = Configure::read('App.additionalEncoding');
  44. if ($run) {
  45. foreach ($url as $key => $value) {
  46. if ($key === '?') {
  47. continue;
  48. }
  49. $value = $this->_encodeUrlPiece($value, $run);
  50. $url[$key] = $value;
  51. }
  52. }
  53. return parent::redirect($url, $status, $exit);
  54. }
  55. /**
  56. * @param mixed Url piece
  57. * @param int $run How many times does the value have to be escaped
  58. * @return mixed Escaped piece
  59. */
  60. protected function _encodeUrlPiece($value, $run) {
  61. if (!is_array($value)) {
  62. for ($i = 0; $i < $run; $i++) {
  63. $value = urlencode($value);
  64. }
  65. return $value;
  66. }
  67. return $this->_encodeUrlPiece($value, $run);
  68. }
  69. /**
  70. * Init Packages class if enabled/included
  71. * @deprecated?
  72. * 2012-12-25 ms
  73. */
  74. public function beforeRender() {
  75. if (class_exists('Packages')) {
  76. Packages::initialize($this, __CLASS__);
  77. }
  78. parent::beforeRender();
  79. }
  80. }