• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter profiler to send output to file instead of display on page?

#1
Sorry, I do not speak English, but I have a question.

I need to save the output of $this->output->enable_profiler(TRUE) into the some file.

Any ideas appreciated.
Reply

#2
Hi gloton,

This trick worked on CI 2.2 (I tried it 5 minutes ago) :

PHP Code:
// Create a new PHP file in your app/libraries folder called "MY_Profiler.php"

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// We are extending the core Profiler Class of CI
class MY_Profiler extends CI_Profiler
{
 
       public function __construct()
 
       {
 
               // Calling CI_Profiler constructor
 
               parent::__construct();
 
       }

 
       // Here we MUST override the original session data compiling function because it's private in CI_Profiler (I just copy/pasted it here without any updates)
 
       private function _compile_session_data()
 
       {
 
               if ( ! isset($this->CI->session))
 
               {
 
                       return;
 
               }

 
               $output '<fieldset id="ci_profiler_csession" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
 
               $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_session_data').'&nbsp;&nbsp;(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_session_data\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_show').'\'?\''.$this->CI->lang->line('profiler_section_hide').'\':\''.$this->CI->lang->line('profiler_section_show').'\';">'.$this->CI->lang->line('profiler_section_show').'</span>)</legend>';
 
               $output .= "<table style='width:100%;display:none' id='ci_profiler_session_data'>";

 
               foreach ($this->CI->session->all_userdata() as $key => $val)
 
               {
 
                       if (is_array($val) OR is_object($val))
 
                       {
 
                               $val print_r($valTRUE);
 
                       }

 
                       $output .= "<tr><td style='padding:5px; vertical-align: top;color:#900;background-color:#ddd;'>".$key."&nbsp;&nbsp;</td><td style='padding:5px; color:#000;background-color:#ddd;'>".htmlspecialchars($val)."</td></tr>\n";
 
               }

 
               $output .= '</table>';
 
               $output .= "</fieldset>";
 
               return $output;
 
       }

 
       // This function is the one we need to override to write output into a file instead of returning it to output (I just updated the return value here)
 
       public function run()
 
       {
 
               $output "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
 
               $fields_displayed 0;

 
               foreach ($this->_available_sections as $section)
 
               {
 
                       if ($this->_compile_{$section} !== FALSE)
 
                       {
 
                               $func "_compile_{$section}";
 
                               $output .= $this->{$func}();
 
                               $fields_displayed++;
 
                       }
 
               }

 
               if ($fields_displayed == 0)
 
               {
 
                       $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee">'.$this->CI->lang->line('profiler_no_profiles').'</p>';
 
               }

 
               $output .= '</div>';

 
               // We try to write the output content to the file at www.yoursite.com/assets/xml/profiler.html (you should modify it to a directory existing in your site directory : take care, the path is relative to the index.php file of CI !)
 
               if(write_file('assets/xml/profiler.html'$output))
 
                       return// It works, nothing to output (if you want to continue outputing profiler : return $output;)
 
               else
                        return 
"<script>alert('Writing profiler output failed.');</script>"// It fails, echo a javascript alert to warn user
 
       }


Before using the write_file function, you should enable the File Helper by editing the autoload.php located in your app/config path :

PHP Code:
$autoload['helper'] = array('file'); 
Reply

#3
I'm extremely grateful.Your solution works perfectly. Thank you for your time and effort in this matter.
Reply

#4
Glad to hear it !

You should probably edit the topic title to add a [Solved] tag so others can benefit of this solution if they need to Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.