MyController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. *
  21. * @overwrite to fix IE cacheing issues
  22. * @return void
  23. * 2012-12-25 ms
  24. */
  25. public function disableCache() {
  26. $this->response->header(array(
  27. 'Pragma' => 'no-cache',
  28. ));
  29. return parent::disableCache();
  30. }
  31. /**
  32. * Fix encoding issues on Apache with mod_rewrite
  33. * Uses Configure::read('App.additionalEncoding') to additionally escape
  34. *
  35. * Tip: Set it to `1` for normal mod_rewrite websites routing directly into webroot
  36. * If you use another setup (like localhost/app/webroot) where you use multiple htaccess files or rewrite
  37. * rules you need to raise it accordingly.
  38. *
  39. * @overwrite to fix encoding issues on Apache with mod_rewrite
  40. * @param string|array $url A string or array-based URL
  41. * @param integer $status Optional HTTP status code (eg: 404)
  42. * @param boolean $exit If true, exit() will be called after the redirect
  43. * @return void
  44. * 2013-03-02 ms
  45. */
  46. public function redirect($url, $status = null, $exit = true) {
  47. $run = Configure::read('App.additionalEncoding');
  48. if ($run && is_array($url)) {
  49. foreach ($url as $key => $value) {
  50. if ($key === '?') {
  51. continue;
  52. }
  53. $value = $this->_encodeUrlPiece($value, $run);
  54. $url[$key] = $value;
  55. }
  56. }
  57. return parent::redirect($url, $status, $exit);
  58. }
  59. /**
  60. * Handles automatic pagination of model records.
  61. *
  62. * @overwrite to support defaults like limit, querystring settings
  63. * @param Model|string $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel')
  64. * @param string|array $scope Conditions to use while paginating
  65. * @param array $whitelist List of allowed options for paging
  66. * @return array Model query results
  67. */
  68. public function paginate($object = null, $scope = array(), $whitelist = array()) {
  69. if ($defaultSettings = (array)Configure::read('Paginator')) {
  70. $this->paginate += $defaultSettings;
  71. }
  72. return parent::paginate($object, $scope, $whitelist);
  73. }
  74. /**
  75. * Additionally encode string to match the htaccess files processing it.
  76. *
  77. * @param mixed Url piece
  78. * @param integer $run How many times does the value have to be escaped
  79. * @return mixed Escaped piece
  80. */
  81. protected function _encodeUrlPiece($value, $run) {
  82. if (!is_array($value)) {
  83. for ($i = 0; $i < $run; $i++) {
  84. $value = urlencode($value);
  85. }
  86. return $value;
  87. }
  88. return $this->_encodeUrlPiece($value, $run);
  89. }
  90. /**
  91. * Init Packages class if enabled/included
  92. * @deprecated?
  93. * 2012-12-25 ms
  94. */
  95. public function beforeRender() {
  96. if (class_exists('Packages')) {
  97. Packages::initialize($this, __CLASS__);
  98. }
  99. parent::beforeRender();
  100. }
  101. }