yusukezzz/slim-debugbar

1.1.2 2015-08-03 03:17 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:00:06 UTC


README

Latest Stable Version Build Status

此中间件将 PHP Debug Bar 添加到 Slim 响应中。

灵感来自 Laravel 4 DebugBar

Screenshot

自定义收集器

  • SlimEnvCollector (收集 slim 模式和版本)
  • SlimViewCollector (收集视图变量)
  • SlimRouteCollector (收集匹配的路由信息)
  • SlimResponseCollector (收集响应头和cookie)

DebugBar 默认收集器

  • SlimCollector
  • PhpInfoCollector
  • ConfigCollector
  • RequestDataCollector
  • TimeDataCollector
  • MemoryCollector

安装

在您的 composer.json 中要求此包

"yusukezzz/slim-debugbar": "1.*"

示例

<?php
require '/path/to/vendor/autoload.php';
$config = [];
// if you want to capture ajax requests, set instance of StorageInterface implemented.
// $config['debugbar.storage'] = new \DebugBar\Storage\FileStorage('/path/to/storage');
$slim = new \Slim\Slim($config);
$debugbar = new \Slim\Middleware\DebugBar();
// you can add custom collectors
//  $debugbar->addCollector(new MyCustomCollector());
// or use custom debugbar
//  $debugbar->setDebugBar(new MyCustomDebugBar());
$slim->add($debugbar);
$slim->get('/', function()
{
    echo 'Hello world!';
});
$slim->run();

注意

  • 请使用真实的 httpd (apache, nginx 等...)
    • PHP 内置服务器不支持。
  • 可用于 AJAX 捕获的存储
    • 文件系统、PDO 和 Redis
    • 更多信息,请参阅 官方文档
  • 重定向数据堆栈
    • 仅支持 PHP 原生会话(需要 session_start())
    • 如果您想使用自己的会话管理器,您应该实现 DebugBar\HttpDriverInterface。
  • 为 DebugBar 保留的路由
    • /_debugbar/fonts/:file
      • 用于 fontawesome 文件
    • /_debugbar/resources/:file
      • 用于 css、javascript
    • /_debugbar/openhandler
      • 用于之前收集的数据集

自定义会话管理器示例

<?php
require '/path/to/vendor/autoload.php';
class MyHttpDriver implements \DebugBar\HttpDriverInterface
{
    protected $session;
    protected $response;
    public function __construct(YourSessionManager $session, \Slim\Http\Response $response)
    {
        $this->session = $session;
        $this->response = $response;
    }
    public function setHeaders(array $headers)
    {
        foreach ($headers as $key => $val) {
            $this->response->header($key, $val);
        }
    }
    public function isSessionStarted()
    {
        return $this->session->isStarted();
    }
    // You should implement other methods too
}
$slim = new \Slim\Slim();
$slim->container->singleton('session', function()
{
    return new YourSessionManager();
});
$driver = new MyHttpDriver($slim->session, $slim->response);
$debugbar = new \Slim\Middleware\DebugBar($driver);
$slim->add($debugbar);
$slim->get('/', function()
{
    echo 'Hello world!';
});
$slim->get('/redirect', function() use ($slim)
{
    $slim->response->redirect('/');
});
$slim->run();

Q&A

  • 问题。浏览器控制台中的错误:Resource interpreted as Font but transferred with MIME type text/html

许可

MIT