[原创中文翻译]symfony askeet24:第十一天,订阅。
October 5, 2007 – 7:53 pm[欢迎转载,转载请注名出处http://watir.cn。本文英文版权归symfony官方网站所有]
Popular questions feed
订阅流行问题
Link to the feed in the head
把订阅链接放到顶部
What we want is an RSS popular questions feed inserted in the <head> of the global layout. The resulting HTML should look like:
我们需要的是在布局页面的<head>旁加上到流行问题的RSS订阅链接。HTML代码看起来像:
<link rel="alternate" type="application/rss+xml" title="Popular questions on askeet" href="http://askeet/frontend_dev.php/feed/popular” />
To do this, open the layout.php and add in the <head>:
为了实现这个功能,打开layout.php,把这段加到<head>:
<?php echo auto_discovery_link_tag('rss', 'feed/popular') ?>
That’s all. The auto_discovery_link_tag helper (autoloaded with the AssetHelper.php helper library) transforms the module/action into a site URI, passing by the routing engine.
这就够了。auto_discovery_link_tag helper(AssetHelper.php helper库自动载入)转化了module/action为一个站点URL,靠路由引擎传递。
Install the plug-in
安装插件
Symfony provides a sfFeed plug-in that automates most of the feed generation. To install it, you will use the symfony command line.
symfony提供了sfFeed插件,自动完成大部分订阅功能。命令安装。
$ symfony plugin-install local symfony/sfFeed
This installs the classes of the plug-in in the askeet/lib/symfony/plugins/ directory, because the local option tells symfony to install the plug-in for the current application only. You could have installed it for all your projects by replacing local by global.
插件被安装到askeet/lib/symfony/plugins/目录下,由于本地设置让symfony仅安装插件到当前项目程序目录。你可以通过修改全局设置把它安装到你所有的项目里。
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, read the plug-in chapter of the symfony book.
如果想知道更多关于插件知识,如何在框架里实现扩展和如何把插件封装应用到你的项目里,看看symfony book的plug-in章节。
Don’t forget to clear the cache since the project lib/ forlder was modified because of the plugin. By the way, if you experiment problems with the plugin-install command (which will probably happen if you don’t use a PEAR-installed version of symfony), copy the files located in the lib/plugins/symfony/plugins/sfFeed/ directory from the SVN repository.
别忘记清除缓存。顺便提一下,如果安装插件出现问题(如果你没有使用PEAR-install安装symfony,可能就会出问题),把askeet SVN目录里lib/plugins/symfony/plugins/sfFeed/下的文件拷贝到本地。
We will talk about this sfFeed class later. But first, we need to write a few lines of code.
我们后面讨论一下sfFeed类。但是首先,我们需要写几行代码。
Create the action
建立动作
The feed points to a popular action of the feed action. To create it, type:
订阅模块创建。输入命令:
$ symfony init-module frontend feed
Then edit the askeet/apps/frontend/modules/feed/actions/action.class.php and add in the following method:
修改askeet/apps/frontend/modules/feed/actions/action.class.php增加几个方法:
public function executePopular()
{
// questions
$c = new Criteria();
$c->addDescendingOrderByColumn(QuestionPeer::INTERESTED_USERS);
$c->setLimit(sfConfig::get('app_feed_max'));
$questions = QuestionPeer::doSelectJoinUser($c);
//
$feed = sfFeed::newInstance('rss201rev2');
//
// channel
$feed->setTitle('Popular questions on askeet');
$feed->setLink(‘@homepage’);
$feed->setDescription(’A list of the most popular questions asked on the askeet site, rated by the community.’);
//
// items
$feed->setFeedItemsRouteName(‘@question’);
$feed->setItems($questions);
//
$this->feed = $feed;
}
Define the app_feed_max_question custom parameter in your askeet/apps/frontend/config/app.yml configuration file:
在askeet/apps/frontend/config/app.yml配置文件里定义app_feed_max_question自定义参数:
all:
feed:
max: 10
Change the view configuration
改变视图配置
By default, the result of our feed/popular action will be decorated by the layout, and will have a text/html content-type. That’s not what we want. So create a view.yml in the askeet/apps/frontend/modules/feed/config/ directory containing:
默认情况下,feed/popular动作的结果会在布局界面显示,也会有文本或者html格式。这不是我们想要的。所以在askeet/apps/frontend/modules/feed/config/下创建一个view.yml:
all:
has_layout: off
template: feed
This deactivates the decorator and forces the output template to feedSuccess.php, whatever the action.
不论是哪个动作,强行输出到feedSuccess.php模板。
Write the template
写模板
That’s because the template is very simple and can be reused for other feeds. Just write this simple askeet/apps/frontend/modules/feed/templates/feedSuccess.php template:
由于模板很简单而且可以被其他订阅动作重用。写个简单的askeet/apps/frontend/modules/feed/templates/feedSuccess.php模板:
<?php echo $feed->getFeed() ?>
Test it
测试一下
Now clear the cache (because the configuration has changed), refresh any page of the site, and notice the feed icon of your favorite web browser. Check the feed by requesting manually:
清除缓存(配置被改了),刷新任何一个页面,注意浏览器里feed图标。手动检查feed,访问下面的URL:
The result is:
结果是:
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Popular questions on askeet</title>
<link>http://askeet/frontend_dev.php/</link>
<description>A list of the most popular questions asked on the askeet site, rated by the community.</description>
<language>en</language>
<item>
<title>What can I offer to my step mother?</title>
<description>My stepmother has everything a stepmother is usually offered
(watch, vacuum cleaner, earrings, [del.icio.us](http://del.icio.us) account).
Her birthday comes next week, I am broke, and I know that
if I don’t offer her something *sweet*, my girlfriend
won’t look at me in the eyes for another month.</description>
<link>http://askeet/frontend_dev.php/question/what-can-i-offer-to-my-step-mother</link>
<guid>11</guid>
<pubDate>Sat, 10 Dec 2005 09:44:11 +0100</pubDate>
</item>
<item>
<title>What shall I do tonight with my girlfriend?</title>
<description>We shall meet in front of the __Dunkin’Donuts__ before dinner,
and I haven’t the slightest idea of what I can do with her.
She’s not interested in _programming_, _space opera movies_ nor _insects_.
She’s kinda cute, so I __really__ need to find something
that will keep her to my side for another evening.</description>
<link>http://askeet/frontend_dev.php/question/what-shall-i-do-tonight-with-my-girlfriend</link>
<guid>10</guid>
<author>fp@example.com (Fabien Potencier)</author>
<pubDate>Sat, 10 Dec 2005 09:44:11 +0100</pubDate>
</item>
<item>
<title>How can I generate traffic to my blog?</title>
<description>I have a very swell blog that talks
about my class and mates and pets and favorite movies.</description>
<link>http://askeet/frontend_dev.php/question/how-can-i-generate-traffic-to-my-blog</link>
<guid>12</guid>
<author>fz@example.com (François Zaninotto)</author>
<pubDate>Sat, 10 Dec 2005 09:44:12 +0100</pubDate>
</item>
</channel>
</rss>
That fast?
够快吗?
The magic
魔术般的效果
Now you might say: how did symfony know where to find the question’s author, his/her email, and how did symfony guess about the URI to a question detail? The answer is: That’s magic.
现在你会问:symfony怎么找到问题的作者呢和他/她的email呢?symfony怎么猜测URL对应具体的问题呢?答案是:这是魔术般的效果。
If you don’t believe in magic, then come beyond the curtain and meet the sfFeed class. This class is able to interpret the names of the methods of the object that is passed as a parameter to its ->setItems() methods. The Question object has a ->getUser() method, so it is used to find the author of a question. The User object has a ->getEmail() method, so this one is also used to determine the author’s email. And the rule name passed to the ->setFeedItemsRouteName() method is:
如果你不信,深入看看sfFeed类。这个类能翻译被当作参数传递到->setItems()方法的对象方法名。Question对象有->getUser()方法,那么这个方法就被用来找到问题的作者。User对象有->getEmail()函数,那么就被用来找到用户的email。被传递到->setFeedItemsRouteName()方法的路由规则是:
question:
url: /question/:stripped_title
param: { module: question, action: show }
It contains a stripped_title parameter, so the ->getStrippedTitle() method of the Question object is called to determine the question URI.
它包含stripped_title参数,所以Question对象的->getStrippedTitle()函数被调用来获得问题的URL。
All that happens because the getter method names make sense - and the sfFeed class understands objects designed that way. The inference mechanisms of this class are described in detail in the feed chapter of the symfony book - refer to it to see how to ask, for instance, a feed without email addresses even if a ->getEmail() method exists for the object’s author
所有的一切都因为getter方法命名变得有意义——sfFeed类就这么设计的。在symfony book的feed章节详细介绍了这个类的设计原理——看看怎么使用,例如,即使->getEmail()函数存在,数据库没有会员的email地址,订阅也能用。
The view of the feed has a XML content-type, so symfony will be smart enough not to add the web debug toolbar to it (otherwise the XML would’nt be valid anymore). If you ever need to disable the web debug toolbar manually, you can always call:
订阅界面用了XML格式,symfony没有给它提供调试工具栏(XML不一定都好用)。如果你想手动禁止显示调试工具栏,你需要调用:
sfConfig::set('sf_web_debug', false);
(find more about the web debug toolbar in the debug chapter of the symfony book).
(在symfony book的debug章节有更多信息)。
Interface improvements
界面改进
Routing
路由
The URL of a feed is as important as a regular one, so append the following to the routing.yml:
订阅的URL重要到需要一个严格的格式,在routing.yml加上:
# feeds
feed_popular_questions:
url: /feed/popular
param: { module: feed, action: popular }
RSS image
RSS订阅图片
Whenever a link to a list had a corresponding field, a nice RSS icon is displayed, together with a link to the RSS. As this will happen quite a few times, create a link_to_feed() function in the GlobalHelper.php:
无论何时,订阅链接都有相似的地方,漂亮的RSS图标显示出来,链接到RSS上。很快这一切都会实现,在GlobalHelper.php里创建link_to_feed()方法:
function link_to_feed($name, $uri)
{
return link_to(image_tag('feed.gif', array('alt' => $name, 'title' => $name, 'align' => 'absmiddle')), $uri);
}
You will find the feed.gif image in the SVN repository.
你可以在SVN库里发现feed.gif图片。
Now, edit the modules/sidebar/templates/defaultSuccess.php as follows:
修改module/sidebar/templates/defaultSuccess.php如下:
<li><?php echo link_to('popular questions', ‘@popular_questions’) ?> <?php echo link_to_rss(’popular questions’, ‘@feed_popular_questions’) ?></li>
2 Responses to “[原创中文翻译]symfony askeet24:第十一天,订阅。”
sfFeed 停止支持了,请问sfFeed2Plugin这插件怎样用的?
By hillmen on Aug 15, 2008
呵呵,毕竟我们在用人家的框架开发,如果人家不支持了,我们自然就需要换一种实现方式了… …
By He Luan on Aug 15, 2008