8
7

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 5 years have passed since last update.

Laravel4 アプリケーション動作環境の設定

Last updated at Posted at 2014-06-26

detectEnvironment メソッドの変更

bootstrap/start.php
$env = $app->detectEnvironment(function() {

	return array_key_exists('APPLICATION_ENV', $_SERVER)
		? $_SERVER['APPLICATION_ENV'] : 'local';

});

アプリケーション動作環境を環境変数に設定する

/etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
...
    SetEnv APPLICATION_ENV development
...
</VirtualHost>

上記は、httpd.conf でもいいし、.htaccess に書いてもOK

シェルから php artisan env を実行すると、設定した環境が確認できる。

動作環境ごとの設定を config ディレクトリに置く

app/config の下に、APPLICATION_ENV の値に対応したディレクトリを用意する。


app/config
     +----- /local
     +----- /development
     .
     .
     .
app/config/local/app.php
<?php
return array(
    'debug' => true,
    'url' => 'http://localhost',
    'timezone' => 'Asia/Tokyo',
    'locale' => 'ja'
);
app/config/development/app.php
<?php
return array(
    'debug' => true,
    'url' => 'http://dev.example.com',
    'timezone' => 'Asia/Tokyo'.
    'locale' => 'ja'
);

などなど

同様に app/config/環境名/database.php で、データベース・サーバーへの接続先の指定を環境ごとに変更できる。

app/config/local/database.php
<?php
return array(
    'connections' => array(
        'mysql => array(
            'driver' => 'mysql',
            'host' => 'localhost',
            'database' => 'test',
            'username' => 'john',
            'password' => 'lemon',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
    ),
);
app/config/development/database.php
<?php
    'connections' => array(
        'mysql => array(
            'driver' => 'mysql',
            'host' => '192.168.1.35',
            'database' => 'test',
            'username' => 'paul',
            'password' => 'mcDonald',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
    ),
);

注意

環境名で testing はユニットテストのために予約されているので使用不可。それ以外は自由に設定できる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?