module-debug.md 5.01 KB
Newer Older
1 2 3
Debug toolbar and debugger
==========================

4 5 6
Yii2 includes a handy toolbar to aid faster development and debugging as well as debugger. Toolbar displays information
about currently opened page while using debugger you can analyze data collected before.

7 8 9 10 11 12 13 14 15 16 17 18 19
Out of the box it allows you to:

- Quickly getting framework version, PHP version, response status, current controller and action, performance info and
  more via toolbar.
- Browsing application and PHP configuration.
- Browsing request data, request and response headers, session data and environment.
- Viewing, searching, filtering logs.
- View profiling results.
- View database queries.
- View emails sent.

All these are available per request so you can browse past requests as well.

20 21 22
Installing and configuring
--------------------------

docsolver committed
23 24
Add these lines to your config file:

Qiang Xue committed
25
```php
26 27 28 29
'preload' => ['debug'],
'modules' => [
	'debug' => ['yii\debug\Module']
]
docsolver committed
30 31
```

Qiang Xue committed
32 33
> Note: by default the debug module only works when browsing the website from the localhost. If you want to use it
> on a remote (staging) server, add the parameter allowedIPs to the config to whitelist your IP, e.g. :**
34

Qiang Xue committed
35
```php
36 37 38
'preload' => ['debug'],
'modules' => [
	'debug' => [
Qiang Xue committed
39 40
		'class' => 'yii\debug\Module',
		'allowedIPs' => ['1.2.3.4', '127.0.0.1', '::1']
41 42 43
	]
]
```
44

45 46 47 48 49 50 51 52 53 54 55 56
If you are using `enableStrictParsing` URL manager option, add the following to your `rules`:

```php
'urlManager' => [
	'enableStrictParsing' => true,
	'rules' => [
		// ...
		'debug/<controller>/<action>' => 'debug/<controller>/<action>',
	],
],
```

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
### Extra config for logging and profiling

Logging and profiling are simple but very powerful tools that may help you to understand execution flow of both the
framework and the application. These are useful both for development and production environments.

While in production environment you should log only important enough messages manually as described in
[logging guide section](logging.md), in development environment it's especially useful to get execution trace.

In order to get trace messages that help you to understand what happens under the hood of the framework, you need to set
trace level in the config:

```php
return [
	// ...
	'components' => [
		'log' => [
			'traceLevel' => YII_DEBUG ? 3 : 0, // <-- here
```

By default it's automatically set to `3` if Yii is run in debug mode i.e. your `index.php` file contains the following:
77

78
```php
79
defined('YII_DEBUG') or define('YII_DEBUG', true);
80 81 82 83
```

> Note: Make sure to disable debug mode on production since it may have significan performance effect and expose sensible
information to end users.
docsolver committed
84

85 86 87
Creating your own panels
------------------------

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
Both toolbar and debugger are highly configurable and customizable. You can create your own panels that could collect
and display extra data. Below we'll describe a process of creation of a simple custom panel that collects views rendered
during request, shows a number in the toolbar and allows you checking view names in debugger. Below we're assuming
basic application template.

First we need to implement panel class in `panels/ViewsPanel.php`:

```php
<?php
namespace app\panels;

use yii\base\Event;
use yii\base\View;
use yii\base\ViewEvent;
use yii\debug\Panel;


class ViewsPanel extends Panel
{
	private $_viewFiles = [];

	public function init()
	{
		parent::init();
		Event::on(View::className(), View::EVENT_BEFORE_RENDER, function (ViewEvent $event) {
113
			$this->_viewFiles[] = $event->sender->getViewFile();
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
		});
	}


	/**
	 * @inheritdoc
	 */
	public function getName()
	{
		return 'Views';
	}

	/**
	 * @inheritdoc
	 */
	public function getSummary()
	{
		$url = $this->getUrl();
		$count = count($this->data);
		return "<div class=\"yii-debug-toolbar-block\"><a href=\"$url\">Views <span class=\"label\">$count</span></a></div>";
	}

	/**
	 * @inheritdoc
	 */
	public function getDetail()
	{
		return '<ol><li>' . implode('<li>', $this->data) . '</ol>';
	}

	/**
	 * @inheritdoc
	 */
	public function save()
	{
		return $this->_viewFiles;
	}
}
```

The workflow for the code above is the following:

1. `init` is executed before running any controller action. Best place to attach handlers that will collect data.
2. `save` is called after controller action is executed. Data returned is stored in data file. If nothing returned panel
   won't render.
3. Data from data file is loaded into `$this->data`. For toolbar it's always latest data, for debugger it may be selected
   to be read from any previous data file.
4. Toolbar takes its contents from `getSummary`. There we're showing a number of view files rendered. Debugger uses
   `getDetail` for the same purpose.

Now it's time to tell debugger to use our new panel. In `config/web.php` debug configuration is modified to be the
following:

```php
if (YII_ENV_DEV) {
	// configuration adjustments for 'dev' environment
	$config['preload'][] = 'debug';
	$config['modules']['debug'] = [
		'class' => 'yii\debug\Module',
		'panels' => [
			'views' => ['class' => 'app\panels\ViewsPanel'],
		],
	];

// ...
```

That's it. Now we have another useful panel without writing much code.