Welcome Guest, Not a member yet? Register   Sign In
global profiling enable/disable
#1

[eluser]ajsie[/eluser]
i read that you can disable and enable system profiling with:

$this->output->enable_profiler(BOOLEAN);

but why isn't this an option in config.php or somewhere else?

why do i have to put this in a controller?

it should be global like other configuration options?

now im force to always put this in a method if i want to disable it?
#2

[eluser]John_Betong[/eluser]
Try this:
Code:
// config.sys
    define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME']);
    define('PROFILER', LOCALHOST);  

  // MY_Controller
    $this->output->enable_profiler(PROFILER);
 
 
 
#3

[eluser]JoostV[/eluser]
It is good practise to set different settings for your development, staging and productions environments. I do this in index.php, but you can do it anywhere, as longs as it's early in the process. Could even create a hook for it, to make it nice & loosely coupled.
Code:
$rootpath = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR;
if (stristr($rootpath, 'C:\htdocs')) {
    define('C_ENV', 'development');
    error_reporting(E_ALL | E_STRICT);
}
elseif (stristr($rootpath, 'home/testuser/domains/example.com')) {
    define('C_ENV', 'staging');
    error_reporting(0);
}
else {
    define('C_ENV', 'production');
    error_reporting(0);
}

You can use this in your database config too, or anywhere for that matter.

config.php:
Code:
if (C_ENV == 'production') {
    $config['log_threshold'] = 1;
}
else {
    $config['log_threshold'] = 4;
}

database.php
Code:
if (C_ENV == 'development') {
    $active_group = 'development';
}
elseif (C_ENV == 'staging') {
    $active_group = 'staging';
}
else {
    $active_group = 'default';
}

My_Controller.php
Code:
if (C_ENV == 'development') {
    $this->output->enable_profiler(TRUE);
}
#4

[eluser]ajsie[/eluser]
I tried it and it got activated. what's the benefit?

[quote author="John_Betong" date="1270917033"]Try this:
Code:
// config.sys
    define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME']);
    define('PROFILER', LOCALHOST);  

  // MY_Controller
    $this->output->enable_profiler(PROFILER);
 
 
 [/quote]
#5

[eluser]ajsie[/eluser]
This solution rocks! No more manual configuration when moving between different folders.

I put your "check environment" code in a pre_system hook. Then i put $this->output->enable_profiler(); in a controller method. It checks what environment it is and put it to either TRUE or FALSE.

Your solution is so clean. Checked out your website and understands that it's because you've got a lot of experiences.

I would want to learn more best practices using CodeIgniter. Is there books you could recommend for me teaching these kinds of best practices? Or are there websites with this kind of advices?

Thanks!

EDIT: Realized that you cannot have this set in a pre_system hook. Cause your config.php is loaded before any hooks (cause you set enable/disable hook in this file). So i had to put it in index.php.

[quote author="JoostV" date="1270920461"]It is good practise to set different settings for your development, staging and productions environments. I do this in index.php, but you can do it anywhere, as longs as it's early in the process. Could even create a hook for it, to make it nice & loosely coupled.
Code:
$rootpath = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR;
if (stristr($rootpath, 'C:\htdocs')) {
    define('C_ENV', 'development');
    error_reporting(E_ALL | E_STRICT);
}
elseif (stristr($rootpath, 'home/testuser/domains/example.com')) {
    define('C_ENV', 'staging');
    error_reporting(0);
}
else {
    define('C_ENV', 'production');
    error_reporting(0);
}

You can use this in your database config too, or anywhere for that matter.

config.php:
Code:
if (C_ENV == 'production') {
    $config['log_threshold'] = 1;
}
else {
    $config['log_threshold'] = 4;
}

database.php
Code:
if (C_ENV == 'development') {
    $active_group = 'development';
}
elseif (C_ENV == 'staging') {
    $active_group = 'staging';
}
else {
    $active_group = 'default';
}

My_Controller.php
Code:
if (C_ENV == 'development') {
    $this->output->enable_profiler(TRUE);
}
[/quote]
#6

[eluser]JoostV[/eluser]
Thanks for the kind words Smile For CI best practises it pays check out the forums. There are one or two books out there that are worth while, best Google for them. Furthermore, check out the blogs of forum members who come up with particularly clever solutions. Lots of those around Wink
#7

[eluser]John_Betong[/eluser]
[quote author="ajsie" date="1270941173"]I tried it and it got activated. what's the benefit?

[quote author="John_Betong" date="1270917033"]Try this:
Code:
// config.php
    define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME']);
    define('PROFILER', LOCALHOST);  

  // MY_Controller
    $this->output->enable_profiler(PROFILER);
 
 
 [/quote][/quote]

Once LOCALHOST is defined it allows you to easily toggle localhost and online options.

I have this set in my config.php so it uses a single config.php file for both localhost and online. No need to have two separate files or to change the configuration before uploading your changes.

Code:
// config.php
  $config['log_threshold'] = LOCALHOST ? '1' : '0'; // 0 is faster and best ONLINE

LOCALHOST is also good for toggling Google Adsense and setting the different database.php options.

Try to make your application work both for localhost and online without having to change any settings or load different files.
 
 
 
#8

[eluser]Unknown[/eluser]
First of all, create a file named profiler.php in application/hooks directory:

Code:
<?php
// profiler.php
class MY_Profiler
{
    private $CI;
    
    public function enable()
    {
        $this->CI =& get_instance();
        
        $this->CI->output->enable_profiler(TRUE);
    }
}

Second of all, add the following to your application/config/hooks.php file:

Code:
$hook['post_controller'] = array(
    'class'    => 'MY_Profiler',
    'function' => 'enable',
    'filename' => 'profiler.php',
    'filepath' => 'hooks'
);

Finally, edit your application/config/config.php file as:

Code:
$config['enable_hooks'] = TRUE;


What just happened?
We have created a hook, that runs after the controller run. (see more) The hook just enables the profiler, and you do not have add enable_profiler(TRUE) to all of your controllers.

To Disable Profiler
If you decide to disable the profiler, you just comment out the part you added to file applications/config/hooks.php




Theme © iAndrew 2016 - Forum software by © MyBB