Welcome Guest, Not a member yet? Register   Sign In
[4.5.1] Disabling the debug toolbar on certain pages
#1
Question 

Hello,

I upgraded from version 4.4.4 to version 4.5.1, and I encountered a new error. Previously, to prevent the debug toolbar from appearing on a script or the login page, I had implemented the following in the file `\app\Config\Boot\development.php` :


PHP Code:
$page current_url(true)->getSegment(1);
helper('url');

if (
$page == 'script' || $page == 'login') {
  defined('CI_DEBUG') || define('CI_DEBUG'false);
} else {
  defined('CI_DEBUG') || define('CI_DEBUG'true);


However, this no longer works because `current_url` is not accessible on this page anymore. So, I tried to do the same thing a bit further in \app\Config\Events.php:


PHP Code:
$page current_url(true)->getSegment(1);
$page_ignore = [
    'script',
    'login'
];

$test TRUE;

if (
in_array($page$page_ignore)) {
    $test FALSE;
    // echo 'is off';
}

if (
CI_DEBUG && ! is_cli() && $test) {
    // echo 'is on';
    Events::on('DBQuery''CodeIgniter\Debug\Toolbar\Collectors\Database::collect');
    Services::toolbar()->respond();
    // Hot Reload route - for framework use on the hot reloader.
    if (ENVIRONMENT === 'development') {
        Services::routes()->get('__hot-reload', static function () {
            (new HotReloader())->run();
        });
    }



Although the `echo` statements indicate the correct path on the tested pages, the toolbar still opens on the login page.

It must be a simple coding error, but I can't figure it out.

Thank you for your assistance.
Reply
#2

Try 'filter system'. Create new filter, test URL from request and disable Toolbar filter.
Or set except. See https://codeigniter.com/user_guide/incom...a-few-uris
Simple CI 4 project for beginners codeigniter-expenses
Reply
#3

(This post was last modified: 04-15-2024, 11:45 PM by keto.)

But of course!
I've previously implemented similar exceptions for my session management filter.
So, I attempted this in "\app\Config\Filter.php"

PHP Code:
    /**
    * List of filter aliases that are always
    * applied before and after every request.
    *
    * @var array<string, array<string, array<string, string>>>|array<string, list<string>>
    */
    public array $globals = [
        'before' => [
            // 'honeypot',
            // 'csrf',
            // 'invalidchars',
            'session' => ['except' => ['login''login/*''script/*']]
        ],
        'after' => [
            'toolbar' => ['except' => ['login''login/*''script/*']],
            // 'honeypot',
            // 'secureheaders',
        ],
    ]; 


However, unfortunately, the toolbar continues to load on the login page.
Reply
#4

Cannot reproduce on CI 4.5.1.


Code:
--- a/app/Config/Filters.php
+++ b/app/Config/Filters.php
@@ -57,7 +57,7 @@ class Filters extends BaseFilters
        'after' => [
            'pagecache',  // Web Page Caching
            'performance', // Performance Metrics
-            'toolbar',    // Debug Toolbar
+            // 'toolbar',    // Debug Toolbar
        ],
    ];

@@ -74,6 +74,7 @@ class Filters extends BaseFilters
            // 'invalidchars',
        ],
        'after' => [
+            'toolbar' => ['except' => ['login', 'login/*', 'script/*']],
            // 'honeypot',
            // 'secureheaders',
        ],
Reply
#5

Thank you for your help, I've found the solution!
While reviewing your modification, I noticed that the 'extends' of the 'class' wasn't the same in my configuration file. So, I reverted to an original file from version 4.5.1 and forgot to update it, even though it's clearly mentioned at the end of the guide: https://codeigniter4.github.io/userguide...e_450.html

After updating the Filter.php and applying your modification, it works as intended. Thanks!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB