MyController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. App::uses('Controller', 'Controller');
  3. /**
  4. * DRY Controller stuff
  5. */
  6. class MyController extends Controller {
  7. /**
  8. * Fix for asset compress to not run into fatal error loops
  9. */
  10. public function __construct($request = null, $response = null) {
  11. parent::__construct($request, $response);
  12. if (strpos($this->here, '/js/cjs/') === 0 || strpos($this->here, '/css/ccss/') === 0) {
  13. unset($this->request->params['ext']);
  14. }
  15. }
  16. /**
  17. * Add headers for IE8 etc to fix caching issues in those stupid browsers
  18. *
  19. * @overwrite to fix IE cacheing issues
  20. * @return void
  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. * @overwrite to fix encoding issues on Apache with mod_rewrite
  37. * @param string|array $url A string or array-based URL
  38. * @param integer $status Optional HTTP status code (eg: 404)
  39. * @param boolean $exit If true, exit() will be called after the redirect
  40. * @return void
  41. */
  42. public function redirect($url, $status = null, $exit = true) {
  43. $run = Configure::read('App.additionalEncoding');
  44. if ($run && is_array($url)) {
  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. * Handles automatic pagination of model records.
  57. *
  58. * @overwrite to support defaults like limit, querystring settings
  59. * @param Model|string $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel')
  60. * @param string|array $scope Conditions to use while paginating
  61. * @param array $whitelist List of allowed options for paging
  62. * @return array Model query results
  63. */
  64. public function paginate($object = null, $scope = array(), $whitelist = array()) {
  65. if ($defaultSettings = (array)Configure::read('Paginator')) {
  66. $this->paginate += $defaultSettings;
  67. }
  68. return parent::paginate($object, $scope, $whitelist);
  69. }
  70. /**
  71. * Additionally encode string to match the htaccess files processing it.
  72. *
  73. * @param mixed Url piece
  74. * @param integer $run How many times does the value have to be escaped
  75. * @return mixed Escaped piece
  76. */
  77. protected function _encodeUrlPiece($value, $run) {
  78. if (!is_array($value)) {
  79. for ($i = 0; $i < $run; $i++) {
  80. $value = urlencode($value);
  81. }
  82. return $value;
  83. }
  84. return $this->_encodeUrlPiece($value, $run);
  85. }
  86. /**
  87. * Init Packages class if enabled/included
  88. * @deprecated?
  89. */
  90. public function beforeRender() {
  91. if (class_exists('Packages')) {
  92. Packages::initialize($this, __CLASS__);
  93. }
  94. parent::beforeRender();
  95. }
  96. }