The data source in CakePHP is actually a good thing. If you have multiple forms of data sources, such as rest, RSS,
JSON, etc. You can write the CakePHP plug-in to read these data sources. This article will write a data source that reads the RSS format
To explain how to use it.
The code can be downloaded at http://www.loadsys.com/files/rss_source.zip,
Place the decompressed code in/APP/models/datasources and set
APP/config/database. php:
VaR $ codeforestnews = array (
'Datasource '=> 'rss ',
'Feedurl' => 'HTTP: // feeds.feedburner.com/codeforest ',
'Encoding' => 'utf-8 ',
'Cachetime' => '+ 1 Day ',
);
Next, create
/APP/models/codeforest_news.php
Class codeforestnews extends appmodel {
VaR $ usetable = false;
VaR $ usedbconfig = 'codeforestnew ';
}
It is illustrated that datasource is used directly as the data source instead of the tables in the database.
Let's look at the control layer:
Class newscontroller extends appcontroller {
VaR $ name = 'News ';
VaR $ uses = array ('codeforestnew ');
Function Index (){
$ This-> paginate = array (
'Limit' => 10,
'Order' => array (
'Codeforestnews. pubdat' => 'desc ',
),
);
$ This-> set ('News', $ this-> paginate ('codeforestnew '));
}
}
It can be seen, or even sorted.
On the page:
<? PHP foreach ($ news as $ newsitem):?>
<? PHP echo $ HTML-> Link ($ newsitem ['codeforestnew'] ['title'], $ newsitem ['codeforestnews'] ['link']);?> <Br/>
<Em> <? PHP echo $ newsitem ['codeforestnew'] ['pubdate'];?> </Em>
<HR>
<? PHP endforeach;?>
<Div class = "Paging">
<? PHP echo $ paginator-> Prev ('<'. _ ('previous ', true), array (), null, array ('class' => 'Disabled');?>
| <? PHP echo $ paginator-> numbers ();?>
<? PHP echo $ paginator-> next (_ ('Next', true ). '>>', array (), null, array ('class' => 'Disabled');?>
</Div>
Demo effect:
Http://www.codeforest.net/demo/cake_data_sources/news/index/page:2
Check the CakePHP manual. You can see that to write datasources, You can implement one of the following methods:
Create ($ model, $ fields = array (), $ values = array ())
Read ($ model, $ querydata = array ())
Update ($ model, $ fields = array (), $ values = array ())
Delete ($ model, $ id = NULL)