LoginSignup
3
3

More than 5 years have passed since last update.

yii2-debugをローカル以外でも使いたい

Last updated at Posted at 2014-12-11

yii2にはモダンフレームワークらしくイカしたデバッグツールがついてます。

スクリーンショット 2014-12-11 22.48.29.png

しかし標準だとローカルIPからのアクセス限定で、VPSやVagrantで開発しようとした時に動かないので困ってしまいます。

どこかで設定するのかさがしてみると、yii2-debug/Module.phpで直接IPが指定されてるのが伺えます

yii2-debug/Module.php
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
namespace yii\debug;
use Yii;
use yii\base\Application;
use yii\base\BootstrapInterface;
use yii\helpers\Url;
use yii\web\View;
use yii\web\ForbiddenHttpException;
/**
 * The Yii Debug Module provides the debug toolbar and debugger
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Module extends \yii\base\Module implements BootstrapInterface
{
    /**
     * @var array the list of IPs that are allowed to access this module.
     * Each array element represents a single IP filter which can be either an IP address
     * or an address with wildcard (e.g. 192.168.0.*) to represent a network segment.
     * The default value is `['127.0.0.1', '::1']`, which means the module can only be accessed
     * by localhost.
     */
    public $allowedIPs = ['127.0.0.1', '::1']

どこかで値を再代入して解決することもできなくはないはずですが、Yiiのモジュール(コンポーネント)は初期化時にpublicなプロパティをオーバーライドできるという特性があります。なので、モジュールの定義をしている設定ファイルに値を指定することで対象のプロパティを再設定することが出来ます。

config/web.php
$config = [
 if (YII_ENV_DEV) {
     // configuration adjustments for 'dev' environment
     $config['bootstrap'][] = 'debug';
-    $config['modules']['debug'] = 'yii\debug\Module';
+ $config['modules']['debug'] = [
+         'class' => 'yii\debug\Module',
+         'allowedIPs' => ['1.2.3.4', '127.0.0.1', '::1'] // '*'ですべてのIPからのアクセスでも動作するようにできる
+ ];

     $config['bootstrap'][] = 'gii';
     $config['modules']['gii'] = 'yii\gii\Module';

これで1.2.3.4からのアクセスでもyii2-debugが動作するようになります。

DBやキャッシュのコンポーネントも同じように設定することができるのでYii2を使う上ではぜひ覚えておきたい手法です。

3
3
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
3
3