- 拡張子でリソースの種類を判断できるよう、「{app}/config/routes.php」に Router::parseExtensions()を追加します。
- RSS用のデフォルトレイアウトビューファイル「{app}/views/layouts/rss/default.ctp」を追加します。
- データをRSS出力するためのビューファイル「{app}/views/posts/rss/index.ctp」を作成します。
- コントローラーにコンポーネントRequestHandlerを追加します。
- function beforeFilter()を追加します。 rssの時にdebug設定が0でないとContent-Typeがtext/htmlになるようなので、 debug設定を0にしてJSONのときのContent-Typeがapplication/jsonになるようにします。 (4.と5.はapp_controller.phpに設定してしまってもよいかもしれません)
- function index()にRSS出力するデータをセットします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php echo $this ->Rss->header(); if (!isset( $channel )) { $channel = array (); } if (!isset( $channel [ 'title' ])) { $channel [ 'title' ] = $title_for_layout ; } echo $this ->Rss->document( $this ->Rss->channel( array (), $channel , $content_for_layout ) ); ?> |
cake bake project すると、{app}/views/layouts に email,js,json,rss,xmlのフォルダーが 自動的に作成されるので必要はないがzipを展開しただけなら無いので作成が必要。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php /* サイト情報の設定 */ $this ->set( 'channel' , array ( 'title' => 'サイトタイトル' , // サイトのタイトル 'description' => 'サイトの説明' , // サイトの説明 'link' => array ( 'controller' => 'osusume' , 'action' => 'ichiran/tochi' ), ) ); echo $rss ->items( $posts , 'rss_transform' ); function rss_transform( $item ) { return array ( 'title' => $item [ 'Post' ][ 'title' ], 'link' => array ( 'controller' => 'posts' , 'action' => 'view' , 'ext' => 'rss' , $item [ 'Post' ][ 'id' ]), 'guid' => array ( 'controller' => 'posts' , 'action' => 'view' , 'ext' => 'rss' , $item [ 'Post' ][ 'id' ]), 'description' => strip_tags ( $item [ 'Post' ][ 'abstract' ]), 'pubDate' => $item [ 'Post' ][ 'created' ], ); } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php // {app}/controllers/posts_controller.php class PostsController extends AppController { var $name = 'Posts' ; var $helpers = array ( 'Html' , 'Form' ); var $components = array ( 'RequestHandler' ); function beforeFilter() { if ( $this ->RequestHandler->ext == 'rss' ) { Configure::write( 'debug' , 0); } } function index() { if ( $this ->RequestHandler->ext == 'rss' ) { $posts = $this ->Post->find( 'all' , array ( 'limit' => 20, 'order' => 'Post.created' )); $this ->set(compact( 'posts' )); } } } ?> |