Artikel ini dirunut berdasarkan Jobeet Tutorial, yang dibuat oleh Fabien Potencier, untuk Symfony 1.4.
Feed RSS dengan Symfony 2
Akan sangat membantu pengguna jika kita menyediakan fitur RSS feed yang dapat memberikan informasi yang update kepada pengunjung. Ok, langkah pertamanya buatlah template terlebih dahulu. Dan untuk feed tersebut kita akan menggunakan template XML. Buat file index.atom.twigsrc/Ibw/JobeetBundle/Resources/views/Job/index.atom.twig
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Jobeet</title>
<subtitle>Latest Jobs</subtitle>
<link href="" rel="self"/>
<link href=""/>
<updated></updated>
<author><name>Jobeet</name></author>
<id>Unique Id</id>
<entry>
<title>Job title</title>
<link href="" />
<id>Unique id</id>
<updated></updated>
<summary>Job description</summary>
<author><name>Company</name></author>
</entry>
</feed>
=======================================
Tambahkan link RSS feed di footer web kita
src/Ibw/JobeetBundle/Resources/views/layout.html.twig
<!-- ... -->
<li class="feed"><a href="{{ path('ibw_job', {'_format': 'atom'}) }}">Full feed</a></li>
<!-- ... -->
=========================================
Tambahkan tag <link> di bagian head yang akan membantu browser untuk mendeteksi keberadaan RSS di web kita.
src/Ibw/JobeetBundle/Resources/views/layout.html.twig
<!-- ... -->
<link rel="alternate" type="application/atom+xml" title="Latest Jobs" href="{{ url('ibw_job', {'_format': 'atom'}) }}" />
<!-- ... -->
============================================
Ubah method index di JobController agar bisa menyesuaikan dengan template yang diminta oleh url.
src/Ibw/JobeetBundle/Controller/JobController.php
// ...
$format = $this->getRequest()->getRequestFormat();
return $this->render('IbwJobeetBundle:Job:index.'.$format.'.twig', array(
'categories' => $categories
));
// ...
============================================
Ubah template header di atom dengan kode berikut:
src/Ibw/JobeetBundle/Resources/views/Job/index.atom.twig
<!-- ... -->
<title>Jobeet</title>
<subtitle>Latest Jobs</subtitle>
<link href="{{ url('ibw_job', {'_format': 'atom'}) }}" rel="self"/>
<link href="{{ url('ibw_jobeet_homepage') }}"/>
<updated>{{ lastUpdated }}</updated>
<author><name>Jobeet</name></author>
<id>{{ feedId }}</id>
<!-- ... -->
===========================================
Kita harus mengirimkin id job yang paling terbaru, untuk itu ubah method indexAction menjadi berikut ini:
src/Ibw/JobeetBundle/Controller/JobController.php
// ...
$latestJob = $em->getRepository('IbwJobeetBundle:Job')->getLatestPost();
if($latestJob) {
$lastUpdated = $latestJob->getCreatedAt()->format(DATE_ATOM);
} else {
$lastUpdated = new \DateTime();
$lastUpdated = $lastUpdated->format(DATE_ATOM);
}
$format = $this->getRequest()->getRequestFormat();
return $this->render('IbwJobeetBundle:Job:index.'.$format.'.twig', array(
'categories' => $categories,
'lastUpdated' => $lastUpdated,
'feedId' => sha1($this->get('router')->generate('ibw_job', array('_format'=> 'atom'), true)),
));
// ...
===========================================
Untuk mendapatkan postingan terakhir dari job kita perlu menambahkan method getLatestPost di JobRepository.php
src/Ibw/JobeetBundle/Repository/JobRepository.php
// ...
public function getLatestPost($category_id = null)
{
$query = $this->createQueryBuilder('j')
->where('j.expires_at > :date')
->setParameter('date', date('Y-m-d H:i:s', time()))
->andWhere('j.is_activated = :activated')
->setParameter('activated', 1)
->orderBy('j.expires_at', 'DESC')
->setMaxResults(1);
if($category_id) {
$query->andWhere('j.category = :category_id')
->setParameter('category_id', $category_id);
}
try{
$job = $query->getQuery()->getSingleResult();
} catch(\Doctrine\Orm\NoResultException $e){
$job = null;
}
return $job;
}
// ...
=====================================
Untuk menggenerate data feednya gunakan kode berikut:
src/Ibw/JobeetBundle/Resources/views/Job/index.atom.twig
{% for category in categories %}
{% for entity in category.activejobs %}
<entry>
<title>{{ entity.position }} ({{ entity.location }})</title>
<link href="{{ url('ibw_job_show', { 'id': entity.id, 'company': entity.companyslug, 'location': entity.locationslug, 'position': entity.positionslug }) }}" />
<id>{{ entity.id }}</id>
<updated>{{ entity.createdAt.format(constant('DATE_ATOM')) }}</updated>
<summary type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
{% if entity.logo %}
<div>
<a href="{{ entity.url }}">
<img src="http://{{ app.request.host }}/uploads/jobs/{{ entity.logo }}" alt="{{ entity.company }} logo" />
</a>
</div>
{% endif %}
<div>
{{ entity.description|nl2br }}
</div>
<h4>How to apply?</h4>
<p>{{ entity.howtoapply }}</p>
</div>
</summary>
<author><name>{{ entity.company }}</name></author>
</entry>
{% endfor %}
{% endfor %}
=====================================
No comments:
Post a Comment