当前位置:Gxlcms > PHP教程 > 我把ci的loader函数改了下大神帮我看看?

我把ci的loader函数改了下大神帮我看看?

时间:2021-07-01 10:21:17 帮助过:28人阅读

<无详细内容>
  1. protected function _ci_load($_ci_data) {
  2. // Set the default data variables
  3. foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val) {
  4. $$_ci_val = (!isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
  5. }
  6. $file_exists = FALSE;
  7. $_ci_ext = '.html';
  8. // Set the path to the requested file
  9. if ($_ci_path != '') {
  10. $_ci_x = explode('/', $_ci_path);
  11. $_ci_file = end($_ci_x) . $_ci_ext;
  12. } else {
  13. //可以注释掉下面这一行,因为我们已经可以通过路由来自动加载视图
  14. //$_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);//获取视图文件的后缀名,默认是不传递后缀名的
  15. //拼接视图路径
  16. //加载配置,以方便获取mca(m:模块,c:控制器,a:action)的值
  17. $this->config = &load_class('Config', 'core');
  18. $directory_trigger = $this->config->item('directory_trigger');
  19. $controller_trigger = $this->config->item('controller_trigger');
  20. $function_trigger = $this->config->item('function_trigger');
  21. //用户没有配置伪静态的情况下,使用pathinfo
  22. $urlinfo = explode('/', $_SERVER['REQUEST_URI']);
  23. $urlinfo['module'] = @$urlinfo[1] ? $urlinfo[1] : 'home';
  24. $urlinfo['controller'] = @$urlinfo[2] ? $urlinfo[2] : 'index';
  25. $urlinfo['action'] = @$urlinfo[3] ? $urlinfo[3] : 'index';
  26. $m = isset($_GET[$directory_trigger]) && $_GET[$directory_trigger]!='' ? $_GET[$directory_trigger] : $urlinfo['module'];
  27. $c =isset($_GET[$controller_trigger]) && $_GET[$controller_trigger]!='' ? $_GET[$controller_trigger] : $urlinfo['controller'];
  28. $a = isset($_GET[$function_trigger]) && $_GET[$function_trigger] !=''? $_GET[$function_trigger] : $urlinfo['action'];
  29. //特殊处理common/header(包含公共头部/底部)的情况
  30. $slasharr = explode('/', $_ci_view);
  31. if (count($slasharr) > 1) {
  32. $_ci_file = $m . '/' . $slasharr['0'].'/' .$slasharr['1'] . $_ci_ext;
  33. } else {
  34. //默认的视图名称(不包含后缀名)
  35. $view_name = $_ci_view == '' ? $a : $_ci_view; //如果没有传视图的名称,默认使用action的名称
  36. $_ci_file = $m . '/' . $c . '/' . $view_name . $_ci_ext;
  37. }
  38. foreach ($this->_ci_view_paths as $view_file => $cascade) {
  39. if (file_exists($view_file . $_ci_file)) {
  40. $_ci_path = $view_file . $_ci_file;
  41. $file_exists = TRUE;
  42. break;
  43. }
  44. if (!$cascade) {
  45. break;
  46. }
  47. }
  48. }
  49. if (!$file_exists && !file_exists($_ci_path)) {
  50. show_error('Unable to load the requested file: ' . $_ci_file);
  51. }
  52. // This allows anything loaded using $this->load (views, files, etc.)
  53. // to become accessible from within the Controller and Model functions.
  54. $_ci_CI = & get_instance();
  55. foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var) {
  56. if (!isset($this->$_ci_key)) {
  57. $this->$_ci_key = & $_ci_CI->$_ci_key;
  58. }
  59. }
  60. /*
  61. * Extract and cache variables
  62. *
  63. * You can either set variables using the dedicated $this->load_vars()
  64. * function or via the second parameter of this function. We'll merge
  65. * the two types and cache them so that views that are embedded within
  66. * other views can have access to these variables.
  67. */
  68. if (is_array($_ci_vars)) {
  69. $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
  70. }
  71. extract($this->_ci_cached_vars);
  72. /*
  73. * Buffer the output
  74. *
  75. * We buffer the output for two reasons:
  76. * 1. Speed. You get a significant speed boost.
  77. * 2. So that the final rendered template can be
  78. * post-processed by the output class. Why do we
  79. * need post processing? For one thing, in order to
  80. * show the elapsed page load time. Unless we
  81. * can intercept the content right before it's sent to
  82. * the browser and then stop the timer it won't be accurate.
  83. */
  84. ob_start();
  85. // If the PHP installation does not support short tags we'll
  86. // do a little string replacement, changing the short tags
  87. // to standard PHP echo statements.
  88. if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE) {
  89. echo eval('?>' . preg_replace("/;*\s*\?>/", "; ?>", str_replace(' } else {
  90. include($_ci_path); // include() vs include_once() allows for multiple views with the same name
  91. }
  92. log_message('debug', 'File loaded: ' . $_ci_path);
  93. // Return the file data if requested
  94. if ($_ci_return === TRUE) {
  95. $buffer = ob_get_contents();
  96. @ob_end_clean();
  97. return $buffer;
  98. }
  99. /*
  100. * Flush the buffer... or buff the flusher?
  101. *
  102. * In order to permit views to be nested within
  103. * other views, we need to flush the content back out whenever
  104. * we are beyond the first level of output buffering so that
  105. * it can be seen and included properly by the first included
  106. * template and any subsequent ones. Oy!
  107. *
  108. */
  109. if (ob_get_level() > $this->_ci_ob_level + 1) {
  110. ob_end_flush();
  111. } else {
  112. $_ci_CI->output->append_output(ob_get_contents());
  113. @ob_end_clean();
  114. }
  115. }

人气教程排行