PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jobeet/en/14.markdown

https://github.com/rafaelgou/symfony1-docs
Markdown | 414 lines | 344 code | 70 blank | 0 comment | 0 complexity | 94a611063ca4dde5a88c93448200b949 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. Day 14: Feeds
  2. =============
  3. Yesterday, you started developing your first very own symfony application.
  4. Don't stop now. As you learn more on symfony, try to add new features to your
  5. application, host it somewhere, and share it with the community.
  6. Let's move on to something completely different today.
  7. If you are looking for a job, you will probably want to be informed as soon as
  8. a new job is posted. Because it is not very convenient to check the website
  9. every other hour, we will add several job feeds today to keep our Jobeet users
  10. up-to-date.
  11. Formats
  12. -------
  13. The symfony framework has native support for ~formats|Formats~ and
  14. ~mime-types|Mime Types~. This means that the same Model and Controller can
  15. have different ~templates|Templates~ based on the requested format. The
  16. default format is HTML but symfony supports ~several other formats|Built-in
  17. Formats~ out of the box like `txt`, `js`, `css`, `json`, `xml`, `rdf`, or
  18. `atom`.
  19. The format can be set by using the `setRequestFormat()` method of the
  20. ~request|HTTP Request~ object:
  21. [php]
  22. $request->setRequestFormat('xml');
  23. But most of the time, the format is embedded in the URL. In this case, symfony
  24. will set it for you if the special ~`sf_format`~ variable is used in the
  25. corresponding route. For the job list, the list URL is:
  26. http://jobeet.localhost/frontend_dev.php/job
  27. This URL is equivalent to:
  28. http://jobeet.localhost/frontend_dev.php/job.html
  29. Both URLs are equivalent because the routes generated by the
  30. `sfPropelRouteCollection` class have the `sf_format` as the extension and
  31. because `html` is the default format. You can check it for yourself by running
  32. the `app:routes` task:
  33. ![Cli](http://www.symfony-project.org/images/jobeet/1_4/15/cli.png)
  34. Feeds
  35. -----
  36. ### Latest Jobs Feed
  37. Supporting different formats is as easy as creating different templates. To
  38. create an [Atom feed](http://en.wikipedia.org/wiki/Atom_(standard)) for the
  39. latest jobs, create an `indexSuccess.atom.php` template:
  40. [php]
  41. <!-- apps/frontend/modules/job/templates/indexSuccess.atom.php -->
  42. <?xml version="1.0" encoding="utf-8"?>
  43. <feed xmlns="http://www.w3.org/2005/Atom">
  44. <title>Jobeet</title>
  45. <subtitle>Latest Jobs</subtitle>
  46. <link href="" rel="self"/>
  47. <link href=""/>
  48. <updated></updated>
  49. <author><name>Jobeet</name></author>
  50. <id>Unique Id</id>
  51. <entry>
  52. <title>Job title</title>
  53. <link href="" />
  54. <id>Unique id</id>
  55. <updated></updated>
  56. <summary>Job description</summary>
  57. <author><name>Company</name></author>
  58. </entry>
  59. </feed>
  60. >**SIDEBAR**
  61. >Template Names
  62. >
  63. >As `html` is the most common format used for web applications, it can be omitted
  64. >from the template name. Both `indexSuccess.php` and `indexSuccess.html.php`
  65. >templates are equivalent and symfony uses the first one it finds.
  66. >
  67. >Why are default templates suffixed with `Success`? An action can return a value
  68. >to indicate which template to render. If the action returns nothing, it is
  69. >equivalent to the following code:
  70. >
  71. > [php]
  72. > return sfView::SUCCESS; // == 'Success'
  73. >
  74. >If you want to change the suffix, just return something else:
  75. >
  76. > [php]
  77. > return sfView::ERROR; // == 'Error'
  78. >
  79. > return 'Foo';
  80. >
  81. >As seen in a previous day, the name of the template can also be changed by
  82. >using the `setTemplate()` method:
  83. >
  84. > [php]
  85. > $this->setTemplate('foo');
  86. By default, symfony will change the response `~Content-Type~` according to the
  87. format, and for all non-HTML formats, the layout is disabled. For an Atom
  88. feed, symfony changes the `Content-Type` to `application/atom+xml;
  89. charset=utf-8`.
  90. In the Jobeet footer, update the link to the feed:
  91. [php]
  92. <!-- apps/frontend/templates/layout.php -->
  93. <li class="feed">
  94. <a href="<?php echo url_for('@job?sf_format=atom') ?>">Full feed</a>
  95. </li>
  96. The ~internal URI|Internal URI~ is the same as for the `job` list with the
  97. `sf_format` added as a variable.
  98. Add a `<link>` tag in the head section of the layout to allow automatic
  99. discover by the browser of our feed:
  100. [php]
  101. <!-- apps/frontend/templates/layout.php -->
  102. <link rel="alternate" type="application/atom+xml" title="Latest Jobs"
  103. href="<?php echo url_for('@job?sf_format=atom', true) ?>" />
  104. For the link `href` attribute, an ~URL (Absolute)~ is used thanks to the
  105. second argument of the `url_for()` helper.
  106. Replace the Atom template header with the following code:
  107. [php]
  108. <!-- apps/frontend/modules/job/templates/indexSuccess.atom.php -->
  109. <title>Jobeet</title>
  110. <subtitle>Latest Jobs</subtitle>
  111. <link href="<?php echo url_for('@job?sf_format=atom', true) ?>" rel="self"/>
  112. <link href="<?php echo url_for('@homepage', true) ?>"/>
  113. <propel>
  114. <updated><?php echo gmstrftime('%Y-%m-%dT%H:%M:%SZ', JobeetJobPeer::getLatestPost()->getCreatedAt('U')) ?></updated>
  115. </propel>
  116. <doctrine>
  117. <updated><?php echo gmstrftime('%Y-%m-%dT%H:%M:%SZ', Doctrine::getTable('JobeetJob')->getLatestPost()->getDateTimeObject('created_at')->format('U')) ?></updated>
  118. </doctrine>
  119. <author>
  120. <name>Jobeet</name>
  121. </author>
  122. <id><?php echo sha1(url_for('@job?sf_format=atom', true)) ?></id>
  123. <propel>
  124. Notice the usage of `U` as an argument to `getCreatedAt()` to get the date as
  125. a timestamp. To get the date of the latest post, create the `getLatestPost()`
  126. method:
  127. </propel>
  128. <doctrine>
  129. Notice the usage of the `U` as an argument to `format()` to get the date as a
  130. timestamp. To get the date of the latest post, create the `getLatestPost()`
  131. method:
  132. </doctrine>
  133. <propel>
  134. [php]
  135. // lib/model/JobeetJobPeer.php
  136. class JobeetJobPeer extends BaseJobeetJobPeer
  137. {
  138. static public function getLatestPost()
  139. {
  140. $criteria = new Criteria();
  141. self::addActiveJobsCriteria($criteria);
  142. return JobeetJobPeer::doSelectOne($criteria);
  143. }
  144. // ...
  145. }
  146. </propel>
  147. <doctrine>
  148. [php]
  149. // lib/model/doctrine/JobeetJobTable.class.php
  150. class JobeetJobTable extends Doctrine_Table
  151. {
  152. public function getLatestPost()
  153. {
  154. $q = Doctrine_Query::create()
  155. ->from('JobeetJob j');
  156. $this->addActiveJobsQuery($q);
  157. return $q->fetchOne();
  158. }
  159. // ...
  160. }
  161. </doctrine>
  162. The feed entries can be generated with the following code:
  163. [php]
  164. <!-- apps/frontend/modules/job/templates/indexSuccess.atom.php -->
  165. <?php use_helper('Text') ?>
  166. <?php foreach ($categories as $category): ?>
  167. <?php foreach ($category->getActiveJobs(sfConfig::get('app_max_jobs_on_homepage')) as $job): ?>
  168. <entry>
  169. <title>
  170. <?php echo $job->getPosition() ?> (<?php echo $job->getLocation() ?>)
  171. </title>
  172. <link href="<?php echo url_for('job_show_user', $job, true) ?>" />
  173. <id><?php echo sha1($job->getId()) ?></id>
  174. <propel>
  175. <updated><?php echo gmstrftime('%Y-%m-%dT%H:%M:%SZ', $job->getCreatedAt('U')) ?></updated>
  176. </propel>
  177. <doctrine>
  178. <updated><?php echo gmstrftime('%Y-%m-%dT%H:%M:%SZ', $job->getDateTimeObject('created_at')->format('U')) ?></updated>
  179. </doctrine>
  180. <summary type="xhtml">
  181. <div xmlns="http://www.w3.org/1999/xhtml">
  182. <?php if ($job->getLogo()): ?>
  183. <div>
  184. <a href="<?php echo $job->getUrl() ?>">
  185. <img src="http://<?php echo $sf_request->getHost().'/uploads/jobs/'.$job->getLogo() ?>"
  186. alt="<?php echo $job->getCompany() ?> logo" />
  187. </a>
  188. </div>
  189. <?php endif; ?>
  190. <div>
  191. <?php echo simple_format_text($job->getDescription()) ?>
  192. </div>
  193. <h4>How to apply?</h4>
  194. <p><?php echo $job->getHowToApply() ?></p>
  195. </div>
  196. </summary>
  197. <author>
  198. <name><?php echo $job->getCompany() ?></name>
  199. </author>
  200. </entry>
  201. <?php endforeach; ?>
  202. <?php endforeach; ?>
  203. The `getHost()` method of the request object (`$sf_request`) returns the
  204. current host, which comes in handy for creating an absolute link for the company
  205. logo.
  206. ![Feed](http://www.symfony-project.org/images/jobeet/1_4/15/feed.png)
  207. >**TIP**
  208. >When creating a feed, ~debugging|Debug~ is easier if you use command line tools like
  209. >[`curl`](http://curl.haxx.se/) or
  210. >[`wget`](http://www.gnu.org/software/wget/), as you see the actual content of
  211. >the feed.
  212. ### Latest Jobs in a Category Feed
  213. One of the goals of Jobeet is to help people find more targeted jobs. So, we
  214. need to provide a ~feed|Feeds~ for each category.
  215. First, let's update the `category` route to add support for different formats:
  216. [yml]
  217. // apps/frontend/config/routing.yml
  218. category:
  219. url: /category/:slug.:sf_format
  220. class: sfPropelRoute
  221. param: { module: category, action: show, sf_format: html }
  222. options: { model: JobeetCategory, type: object }
  223. requirements:
  224. sf_format: (?:html|atom)
  225. Now, the `category` route will understand both the `html` and `atom` formats.
  226. Update the links to category feeds in the ~templates|Templates~:
  227. [php]
  228. <!-- apps/frontend/modules/job/templates/indexSuccess.php -->
  229. <div class="feed">
  230. <a href="<?php echo url_for('category', array('sf_subject' => $category, 'sf_format' => 'atom')) ?>">Feed</a>
  231. </div>
  232. <!-- apps/frontend/modules/category/templates/showSuccess.php -->
  233. <div class="feed">
  234. <a href="<?php echo url_for('category', array('sf_subject' => $category, 'sf_format' => 'atom')) ?>">Feed</a>
  235. </div>
  236. The last step is to create the `showSuccess.atom.php` template. But as this
  237. feed will also list jobs, we can ~refactor|Refactoring~ the code that
  238. generates the feed entries by creating a `_list.atom.php` partial. As for the
  239. `html` format, ~partials|Partial Templates~ are format specific:
  240. [php]
  241. <!-- apps/frontend/modules/job/templates/_list.atom.php -->
  242. <?php use_helper('Text') ?>
  243. <?php foreach ($jobs as $job): ?>
  244. <entry>
  245. <title><?php echo $job->getPosition() ?> (<?php echo $job->getLocation() ?>)</title>
  246. <link href="<?php echo url_for('job_show_user', $job, true) ?>" />
  247. <id><?php echo sha1($job->getId()) ?></id>
  248. <propel>
  249. <updated><?php echo gmstrftime('%Y-%m-%dT%H:%M:%SZ', $job->getCreatedAt('U')) ?></updated>
  250. </propel>
  251. <doctrine>
  252. <updated><?php echo gmstrftime('%Y-%m-%dT%H:%M:%SZ', $job->getDateTimeObject('created_at')->format('U')) ?></updated>
  253. </doctrine>
  254. <summary type="xhtml">
  255. <div xmlns="http://www.w3.org/1999/xhtml">
  256. <?php if ($job->getLogo()): ?>
  257. <div>
  258. <a href="<?php echo $job->getUrl() ?>">
  259. <img src="http://<?php echo $sf_request->getHost().'/uploads/jobs/'.$job->getLogo() ?>"
  260. alt="<?php echo $job->getCompany() ?> logo" />
  261. </a>
  262. </div>
  263. <?php endif; ?>
  264. <div>
  265. <?php echo simple_format_text($job->getDescription()) ?>
  266. </div>
  267. <h4>How to apply?</h4>
  268. <p><?php echo $job->getHowToApply() ?></p>
  269. </div>
  270. </summary>
  271. <author>
  272. <name><?php echo $job->getCompany() ?></name>
  273. </author>
  274. </entry>
  275. <?php endforeach; ?>
  276. You can use the `_list.atom.php` partial to simplify the job feed template:
  277. [php]
  278. <!-- apps/frontend/modules/job/templates/indexSuccess.atom.php -->
  279. <?xml version="1.0" encoding="utf-8"?>
  280. <feed xmlns="http://www.w3.org/2005/Atom">
  281. <title>Jobeet</title>
  282. <subtitle>Latest Jobs</subtitle>
  283. <link href="<?php echo url_for('@job?sf_format=atom', true) ?>" rel="self"/>
  284. <link href="<?php echo url_for('@homepage', true) ?>"/>
  285. <propel>
  286. <updated><?php echo gmstrftime('%Y-%m-%dT%H:%M:%SZ', JobeetJobPeer::getLatestPost()->getCreatedAt('U')) ?></updated>
  287. </propel>
  288. <doctrine>
  289. <updated><?php echo gmstrftime('%Y-%m-%dT%H:%M:%SZ', Doctrine::getTable('JobeetJob')->getLatestPost()->getDateTimeObject('created_at')->format('U')) ?></updated>
  290. </doctrine>
  291. <author>
  292. <name>Jobeet</name>
  293. </author>
  294. <id><?php echo sha1(url_for('@job?sf_format=atom', true)) ?></id>
  295. <?php foreach ($categories as $category): ?>
  296. <?php include_partial('job/list', array('jobs' => $category->getActiveJobs(sfConfig::get('app_max_jobs_on_homepage')))) ?>
  297. <?php endforeach; ?>
  298. </feed>
  299. Eventually, create the `showSuccess.atom.php` template:
  300. [php]
  301. <!-- apps/frontend/modules/category/templates/showSuccess.atom.php -->
  302. <?xml version="1.0" encoding="utf-8"?>
  303. <feed xmlns="http://www.w3.org/2005/Atom">
  304. <title>Jobeet (<?php echo $category ?>)</title>
  305. <subtitle>Latest Jobs</subtitle>
  306. <link href="<?php echo url_for('category', array('sf_subject' => $category, 'sf_format' => 'atom'), true) ?>" rel="self" />
  307. <link href="<?php echo url_for('category', array('sf_subject' => $category), true) ?>" />
  308. <propel>
  309. <updated><?php echo gmstrftime('%Y-%m-%dT%H:%M:%SZ', $category->getLatestPost()->getCreatedAt('U')) ?></updated>
  310. </propel>
  311. <doctrine>
  312. <updated><?php echo gmstrftime('%Y-%m-%dT%H:%M:%SZ', $category->getLatestPost()->getDateTimeObject('created_at')->format('U')) ?></updated>
  313. </doctrine>
  314. <author>
  315. <name>Jobeet</name>
  316. </author>
  317. <id><?php echo sha1(url_for('category', array('sf_subject' => $category), true)) ?></id>
  318. <?php include_partial('job/list', array('jobs' => $pager->getResults())) ?>
  319. </feed>
  320. As for the main job feed, we need the date of the latest job for a category:
  321. [php]
  322. <propel>
  323. // lib/model/JobeetCategory.php
  324. </propel>
  325. <doctrine>
  326. // lib/model/doctrine/JobeetCategory.class.php
  327. </doctrine>
  328. class JobeetCategory extends BaseJobeetCategory
  329. {
  330. public function getLatestPost()
  331. {
  332. $jobs = $this->getActiveJobs(1);
  333. return $jobs[0];
  334. }
  335. // ...
  336. }
  337. ![Category Feed](http://www.symfony-project.org/images/jobeet/1_4/15/category_feed.png)
  338. See you Tomorrow
  339. ----------------
  340. As with many symfony features, the native format support allows you to add
  341. feeds to your websites without effort.
  342. Today, we have enhanced the job seeker experience. Tomorrow, we will see how
  343. to provide greater exposure to the job posters by providing a Web Service.
  344. __ORM__