MyController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. App::uses('Controller', 'Controller');
  3. /**
  4. * DRY Controller stuff
  5. */
  6. class MyController extends Controller {
  7. /**
  8. * @var array
  9. * @link https://github.com/cakephp/cakephp/pull/857
  10. */
  11. public $paginate = array();
  12. /**
  13. * Fix for asset compress to not run into fatal error loops
  14. */
  15. public function __construct($request = null, $response = null) {
  16. parent::__construct($request, $response);
  17. if (strpos($this->request->here, '/js/cjs/') === 0 || strpos($this->request->here, '/css/ccss/') === 0) {
  18. unset($this->request->params['ext']);
  19. }
  20. }
  21. /**
  22. * Add headers for IE8 etc to fix caching issues in those stupid browsers
  23. *
  24. * @overwrite to fix IE cacheing issues
  25. * @return void
  26. */
  27. public function disableCache() {
  28. $this->response->header(array(
  29. 'Pragma' => 'no-cache',
  30. ));
  31. return parent::disableCache();
  32. }
  33. /**
  34. * Fix encoding issues on Apache with mod_rewrite
  35. * Uses Configure::read('App.additionalEncoding') to additionally escape
  36. *
  37. * Tip: Set it to `1` for normal mod_rewrite websites routing directly into webroot
  38. * If you use another setup (like localhost/app/webroot) where you use multiple htaccess files or rewrite
  39. * rules you need to raise it accordingly.
  40. *
  41. * @overwrite to fix encoding issues on Apache with mod_rewrite
  42. * @param string|array $url A string or array-based URL
  43. * @param integer $status Optional HTTP status code (eg: 404)
  44. * @param boolean $exit If true, exit() will be called after the redirect
  45. * @return void
  46. */
  47. public function redirect($url, $status = null, $exit = true) {
  48. $run = Configure::read('App.additionalEncoding');
  49. if ($run && is_array($url)) {
  50. foreach ($url as $key => $value) {
  51. if ($key === '?') {
  52. continue;
  53. }
  54. $value = $this->_encodeUrlPiece($value, $run);
  55. $url[$key] = $value;
  56. }
  57. }
  58. return parent::redirect($url, $status, $exit);
  59. }
  60. /**
  61. * Handles automatic pagination of model records.
  62. *
  63. * @overwrite to support defaults like limit, querystring settings
  64. * @param Model|string $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel')
  65. * @param string|array $scope Conditions to use while paginating
  66. * @param array $whitelist List of allowed options for paging
  67. * @return array Model query results
  68. */
  69. public function paginate($object = null, $scope = array(), $whitelist = array()) {
  70. if ($defaultSettings = (array)Configure::read('Paginator')) {
  71. $this->paginate += $defaultSettings;
  72. }
  73. return parent::paginate($object, $scope, $whitelist);
  74. }
  75. /**
  76. * Additionally encode string to match the htaccess files processing it.
  77. *
  78. * @param mixed Url piece
  79. * @param integer $run How many times does the value have to be escaped
  80. * @return mixed Escaped piece
  81. */
  82. protected function _encodeUrlPiece($value, $run) {
  83. if (!is_array($value)) {
  84. for ($i = 0; $i < $run; $i++) {
  85. $value = urlencode($value);
  86. }
  87. return $value;
  88. }
  89. return $this->_encodeUrlPiece($value, $run);
  90. }
  91. /**
  92. * Init Packages class if enabled/included
  93. *
  94. * @deprecated?
  95. */
  96. public function beforeRender() {
  97. if (class_exists('Packages')) {
  98. Packages::initialize($this, __CLASS__);
  99. }
  100. parent::beforeRender();
  101. }
  102. }