Symfony 2.8 Jobeet Day 14: Feeds

If you are looking for a job, you will probably want to be informed as soon as a new job is posted. Because it is not very convenient to check the website every other hour, we will add several job feeds here to keep our Jobeet users up-to-date.

Template Formats

Templates are a generic way to render content in any format. And while in most cases you’ll use templates to render HTML content, a template can just as easily generate JavaScript, CSS, XML or any other format.

For example, the same “resource” is often rendered in several different formats. To render an article index page in XML, simply include the format in the template name:

  • XML template name: article/show.xml.twig
  • XML template filename: index.xml.twig

In reality, this is nothing more than a naming convention and the template isn’t actually rendered differently based on its format.

In many cases, you may want to allow a single controller to render multiple different formats based on the “request format”. For that reason, a common pattern is to do the following:

The getRequestFormat on the Request object defaults to html, but can return any other format based on the format requested by the user. The request format is most often managed by the routing, where a route can be configured so that /contact sets the request format to html while /contact.xml sets the format to xml. This can be achieved by using the special _format placeholder in your route definition:

Latest Jobs Feed

Supporting different formats is as easy as creating different templates. To create an Atom feed for the latest jobs, create an app/Resources/job/index.atom.twig template:

In the Jobeet footer, update the link to the feed:

In the JobController change the indexAction to render the template according to the _format:

Replace the Atom template header with the following code:

From the JobController we have to send the lastUpdated and feedId to the template:

To get the date of the latest post, we have to create the getLatestPost() method in the JobRepository:

The feed entries can be generated with the following code:

Latest Jobs in a Category Feed

One of the goals of Jobeet is to help people find more targeted jobs. So, we need to provide a feed for each category.

First, let’s update the links to category feeds in the templates:

app/Resources/views/job/index.html.twig
app/Resources/views/category/show.html.twig

Update the CategoryController showAction to render the corresponding template:

Eventually, create the show.atom.twig template:

app/Resources/views/category/show.atom.twig

That’s it! You now provide feeds for your users with the latest jobs. Fine the code from this day here: https://github.com/dragosholban/jobeet-sf2.8/tree/day14


About the Author

Passionate about web & mobile development. Doing this at IntelligentBee for the last 5 years. If you are looking for custom web and mobile app development, please contact us here and let’s have a talk.