PHP, Yii Framework

Getting Yii2 Debug to Show on User Role Instead of IP

<?php

namespace common\components;

use Yii;
use yii\debug\Module as DebugModule;
use common\models\User;

class Module extends DebugModule
{
    private $_basePath;

    protected function checkAccess()
    {
        $user = Yii::$app->getUser();

        if (
            $user->identity &&
            $user->can('admin')
        ) {
            return true;
        }
        return parent::checkAccess();
    }

    /**
     * Returns the root directory of the module.
     * It defaults to the directory containing the module class file.
     * @return string the root directory of the module.
     */
    public function getBasePath()
    {
        if ($this->_basePath === null) {
            $class = new \ReflectionClass(new yii\debug\Module('debug'));
            $this->_basePath = dirname($class->getFileName());
        }

        return $this->_basePath;
    }
}
Advertisements

7 thoughts on “Getting Yii2 Debug to Show on User Role Instead of IP

  1. tried it and had an error. I removed the line ” return parent::checkAccess(); ” instead and got a flawless response. Thank you for this!

  2. Thanks. Good to have the debug toolbar in Prod for troubleshooting. However, there is something that is not clear for me about how the toolbar work. I’m doing some test, in PROD, with the debug toolbar enable, and the log LEVEL in ERROR ONLY. However, I’m seeing sql queries and INFO messages on the debug toolbar, which are stored in runtime/debug… and logs files in runtime/logs empty. I just want to see ERROR messages in the Toolbar for Prod.. is that possible? Why the app is still logging INFO level in runtime/debug but not in runtime/logs?

    1. It is and this works good for me, I see production errors. I am guessing you turned on the debug module but then turned off the debug constant for Yii2. You need both to see errors

      1. Thanks. I’m seeing error, but I’m also seeing INFO messages, while for Prod, the log level is configured for ERRORs only. Why I’m still seeing INFO messages?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s