0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Concrete CMS: デバッグに便利な リクエストデバッガーコードサンプル

Last updated at Posted at 2024-04-01

Concrete CMS のデバッグを行う際に、サーバーの環境設定がうまくいっているか、Concrete CMS の入っている Apache や Nginx にどのようなリクエストが届いているかを解析したい場合があります。

その際、以下のコードを application/bootstrap/app.php に加えて、リクエストのデバッグを手軽に行え流用にします。

$key === 【ランダムな英数字の文字string】 の英数字の部分はパスワード的な役割をしています。48文字以上のランダムな英数字を生成して文字列 (string) として置き換えてください。

そうすると、以下の URL にアクセスすると、サーバーに届いているリクエストを可視化してくれます。

[Concrete CMS]/ccm/debug/request/[ランダムな英数字の文字string]

Concrete CMS の環境編集、カノニカルURL、キャッシュ設定などの CMS の設定情報を出力します。
追加で Config の値を表示したい場合は、適宜、Config の値を追加してください。

以下のサンプルコードは @hissy がベースを作り、Katz が一部改修を加えたものです。

application/bootstrap/app.php
<?php

if ($app->isInstalled()) {
    /** @var \Concrete\Core\Routing\Router $router */
    $router = $app->make('router');
    $router->get('/ccm/debug/request/{key}', static function ($key) use ($app) {
        # Generate random key strings / ここをランダムな英数字に変換する
        if ($key === ランダムな英数字の文字string) {
            $content = '<dl>';
            $content .= sprintf('<dt>Environment</dt><dd>%s</dd>', $app->environment());
            $content .= sprintf('<dt>Canonical URL</dt><dd>%s</dd>', (string) URL::to('/foo'));
            $content .= sprintf('<dt>Block Cache</dt><dd>%s</dd>', ($app['config']->get('concrete.cache.blocks')) ? t('Yes') : t('No'));
            $content .= sprintf('<dt>Override Cache</dt><dd>%s</dd>', ($app['config']->get('concrete.cache.overrides')) ? t('Yes') : t('No'));
            $content .= sprintf('<dt>Full Page Cache</dt><dd>%s</dd>', ($app['config']->get('concrete.cache.pages')) ? t('Yes') : t('No'));
            $request = Request::getInstance();
            $content .= sprintf('<dt>Request Detail</dt><dd>%s</dd>', nl2br((string) $request));
            $content .= sprintf('<dt>Port</dt><dd>%s</dd>', $request->getPort());
            $content .= sprintf('<dt>Scheme</dt><dd>%s</dd>', $request->getScheme());
            $content .= sprintf('<dt>Secure</dt><dd>%s</dd>', ($request->isSecure()) ? t('Yes') : t('No'));
            $content .= sprintf('<dt>Trusted Proxy</dt><dd>%s</dd>', ($request->isFromTrustedProxy()) ? t('Yes') : t('No'));
            foreach ($request->server->all() as $key => $value) {
                $content .= sprintf('<dt>%s</dt><dd>%s</dd>', $key, $value);
            }
            # Sample Output
            ## Output Config value of `example.api.uri`
            // $content .= sprintf('<dt>Config example (example.api.uri)</dt><dd>%s</dd>', $app['config']->get('example.api.uri'));
            $content .= '</dl>';
            return new Response($content);
        } else {
            return new Response('', Response::HTTP_FORBIDDEN);
        }
    });
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?