1

次のような構成ファイルを作成しました。

$conf['db_hostname'] = "localhost";
$conf['db_username'] = "username";
$conf['db_password'] = "password";
$conf['db_name'] = "sample";
$conf['db_type'] = "mysql";
$conf['db_prefix'] = "exp";

config.php として保存します。

同様に、オートローダーは次のようになります

class autoloader {
    public static $loader;

    public static function init()
    {
        if(self::$loader == NULL) {
            self::$loader = new self();
        }
        return self::$loader;
    }   

    public function __construct()
    {
        spl_autoload_register(array(this, 'library'));
        spl_autoload_register(array(this, 'controller'));
        spl_autoload_register(array(this, 'helper'));
        spl_autoload_register(array($this, 'model'));
    }

    public function library($class) 
    {
        set_include_path(get_include_path() . PATH_SEPARATOR . '/lib');
        spl_autoload_extensions('.php');
        spl_autoload($class);
    }

    public function controller($class)
    {
        set_include_path(get_include_path() . PATH_SEPARATOR . '/controller');
        spl_autoload_extensions('.php');
        spl_autoload($class);
    }

    public function helper($class)
    {
        set_include_path(get_include_path() . PATH_SEPARATOR . '/helper');
        spl_autoload_extensions('.php');
        spl_autoload($class);
    }

    public function model($class)
    {
        set_include_path(get_include_path() . PATH_SEPARATOR . '/model');
        spl_autoload_extensions('.php');
        spl_autoload($class);
    }
} 
  • これらの両方のファイルをどこに配置すればよいですか? ルートフォルダに?
  • $configアプリケーション全体でこれらの s をどのように利用できますか?
  • index.php でそれらをどのように使用するのですか? $config配列用にもクラスを作成する必要がありますか?
  • config.php ファイルへの直接アクセスを拒否するにはどうすればよいですか?
4

1 に答える 1

1

設定ファイルを含める必要があります。require_onceの使用をお勧めします。このコードを、構成変数を必要とするすべてのファイルに追加します。これを行う最良の方法は、コントローラーファイル(通常はindex.php)を使用することです。そうすれば、require_onceを1つのファイルに追加するだけで済みます。

require_once("./config.php");

データベースのパスワードを表示する人のことを心配する必要はありません。すべてのphp変数は純粋にサーバー側であり、明示的にエコーアウトされない限り、クライアントはphpコードや変数を表示できません。

于 2011-10-12T14:28:27.037 に答える