Standard.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ cOvGSa9QPt4189rmC7aLgem5hXPRB+LLhHrBiSlt62c=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.5
  11. */
  12. namespace Cube\Controller\Request;
  13. /**
  14. * request object - for when mod rewrite is not available
  15. *
  16. * Class Standard
  17. *
  18. * @package Cube\Controller\Request
  19. */
  20. class Standard extends AbstractRequest
  21. {
  22. const DEFAULT_MODULE = 'App';
  23. const DEFAULT_CONTROLLER = 'Index';
  24. const DEFAULT_ACTION = 'Index';
  25. /**
  26. *
  27. * set the request uri and format it so that it can be used by the router
  28. * (remove the base url of the application from the uri and remove any GET variables as well)
  29. *
  30. * @1.5: modified this method so that it will work on servers where preg_replace does not return the intended results
  31. *
  32. * @param string $requestUri
  33. *
  34. * @return \Cube\Controller\Request\AbstractRequest
  35. */
  36. public function setRequestUri($requestUri = null)
  37. {
  38. if ($requestUri === null) {
  39. // we can retrieve more ways if needed
  40. // we replace the first occurrence of the base url (used if installing the app in a sub-domain)
  41. if (array_key_exists('REQUEST_URI', $_SERVER)) {
  42. $requestUri = $_SERVER['REQUEST_URI'];
  43. if ($baseUrl = $this->getBaseUrl()) {
  44. $requestUri = str_ireplace('//', '/', $this->_strReplaceFirst($baseUrl, '', $requestUri));
  45. }
  46. }
  47. }
  48. $this->_requestUri = $requestUri;
  49. return $this;
  50. }
  51. /**
  52. *
  53. * get the module from the routed request
  54. *
  55. * @return string
  56. */
  57. public function getModule()
  58. {
  59. if (!$this->_module) {
  60. $this->setModule(self::DEFAULT_MODULE);
  61. }
  62. return $this->_module;
  63. }
  64. /**
  65. *
  66. * get the name of the routed action controller
  67. *
  68. * @return string
  69. */
  70. public function getController()
  71. {
  72. if (!$this->_controller) {
  73. $this->setController(self::DEFAULT_CONTROLLER);
  74. }
  75. return $this->_controller;
  76. }
  77. /**
  78. *
  79. * get the name of the routed controller action
  80. *
  81. * @return string
  82. */
  83. public function getAction()
  84. {
  85. if (!$this->_action) {
  86. $this->setAction(self::DEFAULT_ACTION);
  87. }
  88. return $this->_action;
  89. }
  90. protected function _strReplaceFirst($search, $replace, $subject)
  91. {
  92. $pos = strpos($subject, $search);
  93. if ($pos !== false) {
  94. $subject = substr_replace($subject, $replace, $pos, strlen($search));
  95. }
  96. return $subject;
  97. }
  98. }