Difference between revisions of "Workshop - week 06"
(Added a quick link to the next article in the workbook.) |
|||
(26 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
− | [[Main Page]] >> [[Web Frameworks]] >> [[Web Frameworks - Workbook]] >> Workshop - week 06 | + | [[Main Page]] >> [[CP2132|Web Frameworks]] >> [[Web Frameworks - Workbook|Workbook]] >> Workshop - week 06 |
== Consuming an existing web feed == | == Consuming an existing web feed == | ||
− | First you need to create a new controller, with just one "index" action. | + | First you need to create a new controller (new file!), with just one default "index" action. |
+ | |||
+ | In this action you simply create your feed object by calling the Zend_Feed::import function, which take as a parameter the URL of the web feed (the BBC news feed in this example). | ||
− | |||
You then copy the content of the feed to an array, and pass the array to the view. | You then copy the content of the feed to an array, and pass the array to the view. | ||
Line 55: | Line 56: | ||
</pre> | </pre> | ||
− | You also need to create a view for your new controller/action. | + | You also need to create a view (new file!) for your new controller/action. |
+ | The view simply loops through and displays the array provided by the controller: | ||
<pre> | <pre> | ||
− | <!-- application/views/scripts/feed/index. | + | <!-- application/views/scripts/feed/index.phtml --> |
Feed Entries: <br /> | Feed Entries: <br /> | ||
Line 72: | Line 74: | ||
You can then simply access your consumed feed at the following URL: | You can then simply access your consumed feed at the following URL: | ||
− | http://mi-linux.wlv.ac.uk/~0123456/zend/public/feed/ (please replace 0123456 | + | http://mi-linux.wlv.ac.uk/~0123456/zend/public/feed/ (please replace 0123456 with your own student number!) |
+ | |||
+ | <b> Added By Daniel Hardy 0607197</b><p> | ||
+ | The above url should be ( or at least was for me!) | ||
+ | http://mi-linux.wlv.ac.uk/~0123456/QuickStart/public/feed </p> | ||
+ | |||
+ | What you should see on your screen: | ||
+ | |||
+ | [[Image:feed_2.gif]] | ||
Note: For simplicity and clarity we are importing the feed in the controller, but a feed is an XML file, and as such could (should?) be considered as a data source. In line with MVC principles it would therefore be better practice to move the feed import code to a "model" file. | Note: For simplicity and clarity we are importing the feed in the controller, but a feed is an XML file, and as such could (should?) be considered as a data source. In line with MVC principles it would therefore be better practice to move the feed import code to a "model" file. | ||
Line 78: | Line 88: | ||
== Producing your own web feed == | == Producing your own web feed == | ||
− | Let's create a new "feed" action in | + | Note: You don't want any layouts for this action, as you are displaying pure XML, not an HTML page. One way of achieving this is to change the current layout to very simple layout located in your layout folder (and called "blank.phtml" in this example). |
+ | |||
+ | (application/layouts/scripts/blank.phtml) | ||
+ | |||
+ | <pre> | ||
+ | <?= $this->layout()->content ?> | ||
+ | </pre> | ||
+ | |||
+ | Let's create a new "feed" action in your existing "Guestbook" controller. (see [[Workshop - week 04]] - Models and databases) | ||
+ | |||
+ | The first thing you need to do is to specify that you want to use you "blank" layout for this action. | ||
+ | |||
+ | You then transfer your data from the model to an array, and convert the array to a feed using the Zend_Feed::importArray function. | ||
+ | |||
+ | Easy! | ||
<pre> | <pre> | ||
Line 94: | Line 118: | ||
$array['description'] = 'This is guestbook feed'; | $array['description'] = 'This is guestbook feed'; | ||
$array['charset'] = 'utf8'; | $array['charset'] = 'utf8'; | ||
+ | // You need to change this line to read $array['charset'] = 'utf-8'; (Note the change to the 'utf8' part) | ||
// Loop through db records and add feed items to array | // Loop through db records and add feed items to array | ||
Line 112: | Line 137: | ||
$feed->send(); | $feed->send(); | ||
} | } | ||
+ | </pre> | ||
+ | |||
+ | And of course we need a new view for our new action. It is rather simple, as all it only has to display the feed: | ||
+ | |||
+ | (application/views/scripts/guestbook/feed.phtml) | ||
+ | |||
+ | <pre> | ||
+ | <? | ||
+ | echo $this->feedxml; | ||
</pre> | </pre> | ||
To test your new "feed" action simply visit the following URL: | To test your new "feed" action simply visit the following URL: | ||
− | http://mi-linux.wlv.ac.uk/~0123456/zend/public/guestbook/feed (please replace 0123456 | + | http://mi-linux.wlv.ac.uk/~0123456/zend/public/guestbook/feed (please replace 0123456 with your own student number!) |
+ | |||
+ | <b> Added By Sanjeev Summan 0718721</b><p> | ||
+ | The above url should be ( or at least was for me!) | ||
+ | http://mi-linux.wlv.ac.uk/~0123456/QuickStart/public/guestbook/feed</p> | ||
+ | |||
+ | What you should see on your screen (if you are using Firefox 3 anyway... not sure what feeds look like in other browsers): | ||
+ | |||
+ | [[Image:feed.gif]] | ||
== More on web feeds with Zend == | == More on web feeds with Zend == | ||
+ | |||
* http://framework.zend.com/manual/en/zend.feed.html | * http://framework.zend.com/manual/en/zend.feed.html | ||
+ | |||
+ | == Ready to move on? == | ||
+ | |||
+ | When you're happy you understand what you've done here, take a look at the [[Workshop_-_week_07|Week 7 Workshop]] |
Latest revision as of 11:09, 12 September 2009
Main Page >> Web Frameworks >> Workbook >> Workshop - week 06
Consuming an existing web feed
First you need to create a new controller (new file!), with just one default "index" action.
In this action you simply create your feed object by calling the Zend_Feed::import function, which take as a parameter the URL of the web feed (the BBC news feed in this example).
You then copy the content of the feed to an array, and pass the array to the view.
<?php // application/controllers/FeedController.php class FeedController extends Zend_Controller_Action { protected $_model; public function indexAction() { // Read remote feed try { $rss = Zend_Feed::import('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml'); } catch (Zend_Feed_Exception $e) { // feed import failed echo "Exception caught importing feed: {$e->getMessage()}\n"; exit; } // Initialize the channel data array $channel = array( 'title' => $rss->title(), 'link' => $rss->link(), 'description' => $rss->description(), 'items' => array() ); // Loop over each item and store relevant data foreach ($rss as $item) { $channel['items'][] = array( 'title' => $item->title(), 'link' => $item->link(), 'description' => $item->description() ); } // Pass to view $this->view->entries = $channel['items']; } }
You also need to create a view (new file!) for your new controller/action. The view simply loops through and displays the array provided by the controller:
<!-- application/views/scripts/feed/index.phtml --> Feed Entries: <br /> <dl> <!-- Loop through the entries provided by the controller --> <? foreach ($this->entries as $entry): ?> <dt><?= $this->escape($entry['title']) ?></dt> <dd><?= $this->escape($entry['description']) ?></dd> <? endforeach ?> </dl>
You can then simply access your consumed feed at the following URL: http://mi-linux.wlv.ac.uk/~0123456/zend/public/feed/ (please replace 0123456 with your own student number!)
Added By Daniel Hardy 0607197
The above url should be ( or at least was for me!) http://mi-linux.wlv.ac.uk/~0123456/QuickStart/public/feed
What you should see on your screen:
Note: For simplicity and clarity we are importing the feed in the controller, but a feed is an XML file, and as such could (should?) be considered as a data source. In line with MVC principles it would therefore be better practice to move the feed import code to a "model" file.
Producing your own web feed
Note: You don't want any layouts for this action, as you are displaying pure XML, not an HTML page. One way of achieving this is to change the current layout to very simple layout located in your layout folder (and called "blank.phtml" in this example).
(application/layouts/scripts/blank.phtml)
<?= $this->layout()->content ?>
Let's create a new "feed" action in your existing "Guestbook" controller. (see Workshop - week 04 - Models and databases)
The first thing you need to do is to specify that you want to use you "blank" layout for this action.
You then transfer your data from the model to an array, and convert the array to a feed using the Zend_Feed::importArray function.
Easy!
// application/controllers/GuestbookController.php public function feedAction() { // Change layout to blank.phtml (empty file) $this->_helper->layout->setLayout('blank'); // Set feed values $array['title'] = 'Guestbook feed'; $array['link'] = 'http://www.somewhere.com/'; $array['description'] = 'This is guestbook feed'; $array['charset'] = 'utf8'; // You need to change this line to read $array['charset'] = 'utf-8'; (Note the change to the 'utf8' part) // Loop through db records and add feed items to array $model = $this->_getModel(); foreach($model->fetchEntries() as $one_item) { $array['entries'][] = array( 'title' => $one_item['email'], 'link' => 'http://www.somewhere.com/123', 'description' => $one_item['comment'] ); } // Import rss feed from array $feed = Zend_Feed::importArray($array, 'rss'); // Send http headers and dump the feed $feed->send(); }
And of course we need a new view for our new action. It is rather simple, as all it only has to display the feed:
(application/views/scripts/guestbook/feed.phtml)
<? echo $this->feedxml;
To test your new "feed" action simply visit the following URL: http://mi-linux.wlv.ac.uk/~0123456/zend/public/guestbook/feed (please replace 0123456 with your own student number!)
Added By Sanjeev Summan 0718721
The above url should be ( or at least was for me!) http://mi-linux.wlv.ac.uk/~0123456/QuickStart/public/guestbook/feed
What you should see on your screen (if you are using Firefox 3 anyway... not sure what feeds look like in other browsers):
More on web feeds with Zend
Ready to move on?
When you're happy you understand what you've done here, take a look at the Week 7 Workshop