CRequest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fec\helpers;
  10. use Yii;
  11. /**
  12. * @author Terry Zhao <2358269014@qq.com>
  13. * @since 1.0
  14. */
  15. class CRequest
  16. {
  17. public static function getRequest(){
  18. return Yii::$app->request;
  19. }
  20. # 1.得到post请求类型的数据
  21. public static function post($key = ""){
  22. if($key){
  23. $v = self::getRequest()->post($key);
  24. }else{
  25. $v = self::getRequest()->post();
  26. }
  27. return $v;
  28. }
  29. # 2.设置
  30. public static function set($key,$val){
  31. return self::getRequest()->set($key,$val);
  32. }
  33. # 3.得到get请求类型的数据
  34. public static function get($key = ""){
  35. if($key){
  36. $v = self::getRequest()->get($key);
  37. }else{
  38. $v = self::getRequest()->get();
  39. }
  40. return $v;
  41. }
  42. # 4.得到get 和 post的所有数据。
  43. # 如果一个值在get和post中都存在,则post优先。
  44. public static function param($key = ''){
  45. if($key){
  46. $get = self::get();
  47. $post = self::post();
  48. if(isset($post[$key])){
  49. return $post[$key] ;
  50. }else if(isset($get[$key])){
  51. return $get[$key] ;
  52. }else{
  53. return "";
  54. }
  55. }else{
  56. $get = self::get();
  57. $post = self::post();
  58. return array_merge($get,$post);
  59. }
  60. }
  61. # 5.得到csrfName
  62. public static function getCsrfName(){
  63. return self::getRequest()->csrfParam;
  64. }
  65. # 6.得到csrf对应的值。
  66. public static function getCsrfValue(){
  67. return self::getRequest()->getCsrfToken();
  68. }
  69. # 7.得到csrf的input 的 html
  70. public static function getCsrfInputHtml(){
  71. return '<input class="thiscsrf" type="hidden" value="'.self::getCsrfValue().'" name="'.self::getCsrfName().'" />';
  72. }
  73. # 8.得到csrf对应的字符串
  74. public static function getCsrfString(){
  75. return self::getCsrfName()."=".self::getCsrfValue();
  76. }
  77. }