📜 ⬆️ ⬇️

An example of creating a practical Debug panel in Yii

Writing your own Debug panel in Yii is very simple. Therefore, we will consider a slightly practical (with a call to api PHPStorm) view panel.

There is extensively chewed documentation, with an example of displaying a list of views. Let's bring it into a more practical form.

1. Remove duplicates.
2. Divide the layouts for layouts, and basic ones.
3. Make a link to the Idea API.

As you know, when we click on the link displayed by the getSummary function in the “mini panels”, the panel with the content transmitted by the getDetail function is displayed . For brevity, its code and give:
')
public function getDetail() { $rootDir = Yii::getAlias('@app'); $rootDir = dirname($rootDir); //remove last dir $viewsNormal = []; $viewsLayout = []; //$this->data = array_unique($this->data); //if delete duplicates foreach ($this->data as $view) { $viewFile = str_replace($rootDir.DIRECTORY_SEPARATOR, '', $view); if (strpos($viewFile, 'layouts') !== false) { $viewsLayout[$viewFile]++; } else { $viewsNormal[$viewFile]++; } } //Display $js = <<<JS function _openIDE(elem){ var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", "http://localhost:63342/api/file?file=" + elem.innerHTML, true); xmlhttp.send(); } JS; $content = "<script>$js</script>"; $content .= 'Views:<ol>'; foreach ($viewsNormal as $v => $count) { $content .= '<li>' . $this->link2IDE($v) . ($count > 1 ? " ($count)" : '') . '</li>'; } $content .= '</ol>'; $content .= '<hr>on layout:<ol>'; foreach ($viewsLayout as $v => $count) { $content .= '<li>' . $this->link2IDE($v) . ($count > 1 ? " ($count)" : '') . '</li>'; } $content .= '</ol>'; return $content; } 

If you make a "standard link" (option 1), a new empty window opens in the browser. Therefore, a separate JS function was made.

  private function link2IDE($linkFile) { //Variant 1 //$port = '63342'; //return Html::a($linkFile, "http://localhost:$port/api/file?file=$linkFile&line=1", ['target' => '_top']); //Variant 2 return "<a href='#' onclick='_openIDE(this);return false;'>$linkFile</a>"; } 

If you are not paranoid, a constantly pop-up permission request is annoying, you need to turn on the check mark.


As a result, this form turned out, when you click on a link, the first line of the entry opens in the IDE (intellij, I have IDEA, PHPStorm should work too):


Anticipating possible questions:


PS: We ourselves are not local, so please "proffit and understand" , write in a personal error.

Source: https://habr.com/ru/post/317562/


All Articles