TRAJOIN is an Application to Translate symfony documents Jointly.
home > 1.2/cookbook/en > syndication.txt
[1] Edit ↑TOPアプリケーションが投稿、画像、ニュース、質問もしくは何であれリスト表示をするかどうか、更新率は一ヶ月に一回以上高い場合、ユーザーがフィードアグリゲーターの範囲内からあなたのウェブサイトについて最新の情報を知ることができるようにシンディケーションフィード(RSS、Atomなど)を提供しなければなりません。よいニュースは、オブジェクトモデルがすぐに組み込まれた場合、symfonyはすべてのことをしてくれるフィードビルダープラグインを提供するので開発するための複数行は必要ありません。
この章で見ることが出来る例はPostとAuthorテーブルでシンプルなblogアプリケーションです:
| Post | Author |
|---|---|
| id | id |
| author_id | first_name |
| title | last_name |
| description | |
| body | |
| created_at |
PostクラスはgetStrippedTitle()メソッドによって拡張されます。タイトルをURIで使用できる文字列に変換し、空白をダッシュに、小文字によって大文字を置き換え、すべての特別な文字を除去します:
public function getStrippedTitle()
{
$text = strtolower($this->getTitle());
// すべての単語でない文字列を除去
$text = preg_replace('/\W/', ' ', $text);
// ダッシュですべての空白セクションを置き換える
$text = preg_replace('/\ +/', '-', $text);
// ダッシュを整形する
$text = preg_replace('/\-$/', '', $text);
$text = preg_replace('/^\-/', '', $text);
return $text;
}
Authorクラスは次のようにカスタムの->getName()メソッドによって拡張されます:
public function getName()
{
return $this->getFirstName().' '.$this->getLastName()
}
If you need more details about the way to extend the model, refer to Chapter 8.
routing.ymlは次のルールを含みます:
post:
url: /permalink/:stripped_title
param: { module: post, action: read }
ルーティングシステムについてもっと詳細な情報が必要でしたら9章を参照して下さい。
特別なfeedモジュールは必要な時にビルドされ、すべてのアクションとテンプレートはそれに設置されます。
$ symfony init-module myapp feed
フィードアクションは[Atom][1]フィードを出力しなければなりません。リマインダーとしてすべての情報がAtomフィードに含まれる必要があります。例です:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>The mouse blog</title>
<link href="http://www.myblog.com/" />
<updated>2005-12-11T16:23:51Z</updated>
<author>
<name>Peter Clive</name>
<author_email>pclive@myblog.com</author_email>
</author>
<id>4543D55FF756G734</id>
<entry>
<title>I love mice</title>
<link href="http://www.myblog.com/permalink/i-love-mice" />
<id>i-love-mice</id>
<author>
<name>Peter Clive</name>
<author_email>pclive@myblog.com</author_email>
</author>
<updated>2005-12-11T16:23:51Z</updated>
<summary>Ever since I bought my first mouse, I can't live without one.</summary>
</entry>
<entry>
<title>A mouse is better than a fish</title>
<link href="http://www.myblog.com/permalink/a-mouse-is-better-than-a-fish" />
<id>a-mouse-is-better-than-a-fish</id>
<author>
<name>Bob Walter</name>
<author_email>bwalter@myblog.com</author_email>
</author>
<updated>2005-12-09T09:11:42Z</updated>
<summary>I had a fish for four years, and now I'm sick. They smell.</summary>
</entry>
</feed>
symfonyは大抵のフィード生成を自動化するsfFeedプラグインを提供します。インストールするために、symfonyコマンドを使用して下さい:
$ symfony plugin-install http://plugins.symfony-project.com/sfFeedPlugin
これはplugins/ディレクトリでプラグインのクラスをインストールします。symfonyがそれらをオートロードするために、コンフィギュレーションキャッシュをクリアする必要があります:
$ symfony cc
If you want to learn more about plug-ins, how they extend the framework and how you can package the features that you use across several projects into a plug-in, refer to Chapter 17 of the symfony book.
feedモジュールにおいて、lastPostsアクションを作成して下さい:
public function executeLastPosts()
{
$feed = sfFeed::newInstance('atom1');
$feed->setTitle('The mouse blog');
$feed->setLink('http://www.myblog.com/');
$feed->setAuthorEmail('pclive@myblog.com');
$feed->setAuthorName('Peter Clive');
$c = new Criteria;
$c->addDescendingOrderByColumn(PostPeer::CREATED_AT);
$c->setLimit(5);
$posts = PostPeer::doSelect($c);
foreach ($posts as $post)
{
$item = new sfFeedItem();
$item->setFeedTitle($post->getTitle());
$item->setFeedLink('@permalink?stripped_title='.$post->getStrippedTitle());
$item->setFeedAuthorName($post->getAuthor()->getName());
$item->setFeedAuthorEmail($post->getAuthor()->getEmail());
$item->setFeedPubdate($post->getCreatedAt('U'));
$item->setFeedUniqueId($post->getStrippedTitle());
$item->setFeedDescription($post->getDescription());
$feed->addItem($item);
}
$this->feed = $feed;
}
初めの[factory][2]メソッドは'Atom'フォーマットのためにsfFeedクラスのインスタンスを作成します。sfFeedとsfFeedItemクラスはフィードコンストラクションのために特別に作成されたsymfonyアドオンです。アクションの終わりで、$feed変数はいくつかのsfFeedItemオブジェクトを内包するsfFeedオブジェクトを含みます。オブジェクトを実際のAtomフィードに変換するために、lastPostsSuccess.phpテンプレートはシンプルに次の内容を含みます:
<?php echo $feed->getFeed() ?>
テンプレートはレイアウトによって装飾される必要はありません。加えて結果ページはtext/xmlcontent-typeとして宣言されます。view.ymlをfeedモジュールのconfig/ディレクトリに追加して下さい:
all:
has_layout: off
http_metas:
content-type: text/xml
フィードアグリゲーターから呼び出されるとき、上で説明されたようにアクションの結果はまさにAtomフィードです:
http://www.myblog.com/feed/lastPosts
定義するための多くの情報があるので、itemコンストラクションのためのすべてのセッターの使用は少し面倒かもしれません。連想配列を使用して、symfonyは同じ効果を生み出すショートハンド構文を提供します:
public function executeLastPosts()
{
$feed = sfFeed::newInstance('atom1');
$feed->setTitle('The mouse blog');
$feed->setLink('http://www.myblog.com/');
$feed->setAuthorEmail('pclive@myblog.com');
$feed->setAuthorName('Peter Clive');
$c = new Criteria;
$c->addDescendingOrderByColumn(PostPeer::CREATED_AT);
$c->setLimit(5);
$posts = PostPeer::doSelect($c);
foreach ($posts as $post)
{
$item = array(
'title' => $post->getTitle(),
'link' => '@permalink?stripped_title='.$post->getStrippedTitle(),
'authorName' => $post->getAuthor()->getName(),
'authorEmail' => $post->getAuthor()->getEmail(),
'pubdate' => $post->getCreatedAt(),
'uniqueId' => $post->getStrippedTitle(),
'description' => $post->getDescription(),
);
$feed->addItemFromArray($item);
}
$this->feed = $feed;
}
まさに同じ効果ですが、構文はより明確です。
構築するために使用されるメソッド名として、新しいフィールドは多かれ少なかれ常に同じで、symfonyはまさに次のコードによってのみ同じことをすることが出来ます:
public function executeLastPosts()
{
$feed = sfFeed::newInstance('atom1');
$feed->setTitle('The mouse blog');
$feed->setLink('http://www.myblog.com/');
$feed->setAuthorEmail('pclive@myblog.com');
$feed->setAuthorName('Peter Clive');
$c = new Criteria;
$c->addDescendingOrderByColumn(PostPeer::CREATED_AT);
$c->setLimit(5);
$posts = PostPeer::doSelect($c);
$feed->setFeedItemsRouteName('@permalink');
$feed->setItems($posts);
$this->feed = $feed;
}
正真正銘のマジックだと思いませんか?
sfFeedオブジェクトのマジックPostオブジェクトのゲッター関数は明確なのでsymfonyはそれらを理解することも出来ます。sfFeedクラスはよく組織されたクラスから適切な情報を推定するビルトインメカニズムを持つからです:
titleアイテムを設定するために、getFeedTitle()、getTitle()、getName()もしくは__toStringメソッドを探します。例において`Post`オブジェクトは`getName()`メソッドを持ちます。
linkを設定するためにフィードアイテムルートネームを探します(setFeedItemsRouteName()によって定義されます)。一つある場合、オブジェクトメソッドにあるゲッターを見つけるためにパラメータのためのルートURLで探します。相ではない場合、getFeedLink()、getLink()、getUrl()メソッドを探します。例において、一つのルート名は上記のアクション(`@permalink`)で定義されます。ルーティングルールは`:stripped_title`パラメータを含み、`Post`オブジェクトは`getStripped_Title()`メソッドを持つので、`sfFeed`オブジェクトはリンクするURIを定義することが出来ます。
getFeedAuthorEmailもしくはgetAuthorEmailを探します。そのようなメソッドが存在しない場合、getAuthor()、getUser()もしくはgetPerson()メソッドを探します。返された結果がオブジェクトの場合、このオブジェクト内でgetEmailもしくはgetMailメソッドを探します。例では、`Post`オブジェクトは`getAuthor()`を持ち、`Author`オブジェクトは`getName()`を持ちます。同じ種類のルールは著者の名前とURLのために使用されます。
getFeedPubdate()、getPubdate()、getCreateAt()もしくはgetDate()メソッドを探します。例では、`Post`オブジェクトは`getCreatedAt`を持ちます
同じことがAtomフィード(カテゴリ、要約、ユニークIDなど)の他のあり得るフィールドのために当てはまり、すべての推測メカニズムを発見するためにはsfFeedクラスのソースをブラウズすることをお勧めします。
全部で、PostとAuthorオブジェクトのアクセサがビルドされる方法によって動作するsfFeedの組み込みのショートカットを可能にし、フィードの作成がシンプルになります。
上で説明されたリストにおいて、sfFeedオブジェクトが最初に探すメソッド名が常にgetFeedXXX()であることを確認することが出来ます。このことでシンプルに拡張されたモデルによってフィードアイテムのそぞれのフィールドのためにカスタムの値を指定することが可能です。
例えば、著者のEメールをフィードに公開したくない場合、次のgetFeedAuthorEmail()メソッドをPostオブジェクトに追加して下さい:
public function getFeedAuthorEmail()
{
return '';
}
このメソッドはgetAuthor()メソッドの前に見つかり、フィードは公開者のEメールアドレスをさらしません。
下で説明されたメソッドは他のRSSフィードをビルドするために置き換えることが出来ます。フィードファクトリに与えられたパラメータを変更して下さい:
// RSS 0.91
$feed = sfFeed::newInstance('rssUserland091');
// RSS 2.01
$feed = sfFeed::newInstance('rss201rev2');
Documentation
| [1]: http://en.wikipedia. ... | |
| brtRiver(2009-01-05 04:45:01) | |
| [php] // RSS 0.91 ... | |
| brtRiver(2009-01-05 04:44:54) | |
| The methods described bel ... | |
| brtRiver(2009-01-05 04:44:47) | |
| Use other formats ------- ... | |
| brtRiver(2009-01-05 04:44:39) | |
| This method will be found ... | |
| brtRiver(2009-01-05 04:44:27) | |
| [php] public func ... | |
| brtRiver(2009-01-05 04:44:19) | |
| For instance, if you don& ... | |
| brtRiver(2009-01-05 04:44:10) | |
| In the list presented abo ... | |
| brtRiver(2009-01-05 04:44:00) | |
| Define custom values for ... | |
| brtRiver(2009-01-05 04:43:47) | |
| All in all, the way the a ... | |
| brtRiver(2009-01-05 04:43:38) | |
| The same goes for the oth ... | |
| brtRiver(2009-01-05 04:43:29) | |
| In the example, the ` ... | |
| brtRiver(2009-01-05 04:43:21) | |
| - To set the publicatio ... | |
| brtRiver(2009-01-05 04:43:06) | |
| In the example, the ` ... | |
| brtRiver(2009-01-05 04:42:54) | |
| - To set the author ... | |
| brtRiver(2009-01-05 04:42:44) | |
| In the example, one r ... | |
| brtRiver(2009-01-05 04:42:34) | |
| - To set the `link`, it ... | |
| brtRiver(2009-01-05 04:42:20) | |
| In the example, the ` ... | |
| brtRiver(2009-01-05 04:42:11) | |
| - To set the item `titl ... | |
| brtRiver(2009-01-05 04:42:04) | |
| The getter functions of t ... | |
| brtRiver(2009-01-05 04:41:55) |