プラグインを利用した機能追加
symfonyの特徴の一つは、プラグインを利用した機能追加ができることにあります。
プラグインを見つける
Symfony Webサイトの「Symfony plugins」には、現在利用できるsymfonyプラグインがリストアップされています。例えば次のようなものがあります。
| プラグイン名 | 説明 |
| sfYUIPlugin | Yahoo! UI ライブラリ |
| sfOpenOfficePlugin | OpenOfficeドキュメントを出力可能にする |
| sfThumbnailPlugin | サムネイルを出力する |
| sfDoctrine | Propelの代わりにDoctrine model layerを利用可能にする |
| sfControlPanelPlugin | symfonyプロジェクト管理用のWebインターフェイス |
| sfCryptPlugin | mcryptを利用した暗号化関連機能を提供する |
| sfGuardPlugin | symfonyの標準セキュリティ以上の認証機能を提供する |
| sfFeedPlugin | RSSフィード |
| sfShoppingCartPlugin | ショッピングカート |
| sfSmartyViewPlugin | Smartyテンプレートエンジンを利用可能にする |
プラグインをインストールする
今回はSmartyプラグインを例にとり、インストールしてみます。インストールはsymfony同様に、PEARを用いるのが簡単です。
cd myproject symfony plugin-install http://plugins.symfony-project.com/<プラグイン名> symfony cc
またPEARチャンネルからインストールする場合は
symfony plugin-install <チャンネル名>/<プラグイン名>
とします。
>symfony plugin-install http://plugins.symfony-project.com/sfSmartyViewPlugin >> dir+ C:\Program Files\Apache Softwar...hannel.pear.symfony-project.com >> plugin installing plugin "http://plugi...project.com/sfSmartyViewPlugin" >> pear downloading sfSmartyViewPlugin-0.1.3.tgz ... >> pear Starting to download sfSmartyViewPlugin-0.1.3.tgz (125,463 >> pear bytes) >> pear .. >> pear .. >> pear .. >> pear .. >> pear .. >> pear .. >> pear .. >> pear .. >> pear .. >> pear .. >> pear .. >> pear .. >> pear ....done: 125,463 bytes >> pear install ok: >> pear channel://pear.symfony-project.com/sfSmartyViewPlugin-0.1.3
モジュールを含んだプラグインの設定
一部のプラグインはモジュールをそのまま含んでいます。その場合、「settings.yml」でモジュールを指定する必要があります。
all:
.settings:
enabled_modules: [default, sfMyPluginModule]
プラグインの構造
プラグインは一つのプロジェクト用のフォルダ構造を有しており、次のようになっています。
<プラグイン名>/
config/
*schema.yml (データスキーマ)
*schema.xml
config.php (プラグイン固有の設定)
data/
generator/
sfPropelAdmin
*/ (アドミンジェネレーターのテーマ)
templates/
skeleton/
fixtures/
*.yml (fixturesファイル群)
tasks/
*.php (Pake タスク)
lib/
*.php (クラス群)
helper/
*.php (ヘルパー群)
model/
*.php (モデルクラス群)
modules/
*/ (モジュール)
actions/
actions.class.php
config/
module.yml
view.yml
security.yml
templates/
*.php
validate/
*.yml
web/
* (webリソース)
このフォルダ内のファイル群を変更することは通常なく、カスタマイズは基本的にアプリケーションフォルダ内の「config/app.yml」で行います。
all:
<プラグイン名>:
<設定項目>: <値>
現在のバージョンは「0.1.3」で、まだまだ発展途上のようです。実際にプラグインをインストールした後は、アプリケーションの「config」フォルダ内に「module.yml」を作成し次のように設定します。
all: view_class: sfSmarty
all:
sfSmartyView:
class_path: Smarty/libs (Smartyライブラリの場所)
マニュアルの例を抜粋すると、次のように記述できるようです。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> {include_http_metas} {include_metas} {include_title} <link rel="shortcut icon" href="/favicon.ico" /> </head> <body> {$sf_data->getRaw('sf_content')} </body> </html>
<div> <h2>You did it…</h2> <p>this is the first Smarty template</p> </div>
<?php echo link_to($name="home", $internal_uri="mod1/index") ?>
{link_to name="home" internal_uri="mod1/index"}
| symfonyヘルパー | Smartyプラグイン使用時の記述 |
| <?php include_http_metas() ?> | {include_http_metas} |
| <?php include_metas() ?> | {include_metas} |
| <?php include_title() ?> | {include_title} |
| <?php echo $sf_data->getRaw('sf_content') ?> | {$sf_data->getRaw('sf_content')} |
今回のまとめ
今回はsymfonyの国際化機能とプラグインの使用について紹介しました。次回最終回は、昨今、話題に上ることも多いAjaxプログラミングをsymfonyフレームワーク上で実現する方法について解説します。お楽しみに。
参考文献
国際化機能に関連して、国と言語のコードは以下で調べられます。
- Wikipedia 『ISO 3166-1』
- Wikipedia 『ISO 639』
