configファイルの読み込み
設定ファイルを読み込む / Mojoliciousリファレンス – サンプルコードによるPerl入門
設定(コンフィグ)ファイルを読み込むにはMojolicious::Plugin::Configを利用します。
というように、Mojoliciousアプリで設定ファイルを使う事ができる。例えば etc/MyApp.conf を読み込むには以下のような感じ。 stash_keyはオプションで設定できる(デフォルトはconfig?)
etc/MyApp.conf
{ # MyApp config # サービス名 SARVICE_NAME => 'hogehoge', # 配列のリファレンス等 CATEGORY => [0,1,2,3,4], }
MyApp.pm
package MyApp; sub startup { my $self = shift; # コンフィグファイル読み込み my $config = $self->plugin('config', { file => 'etc/MyApp.conf', stash_key => 'conf' }); .... }
読み込みはリンク先に詳しく書かれている。
configで設定された値を使う
コントローラーとかで使う
sub index { my $self = shift; #my $config = $self->stash('conf'); my $config = $self->config; my $service_name = $config->{SARVICE_NAME}; # or my $service_name = $self->config('SARVICE_NAME'); .... }
テンプレートで使う
<%= stash('conf')->{SARVICE_NAME} %> <%= config->{SARVICE_NAME} %> <%= config('SARVICE_NAME') %>
オブジェクト指向的には config()で取得したほうがいいのかな?
テンプレートで使うには
がタイプが一番少ないようだ。
カテゴリー: Mojolicious - Trackback Uri

