- 拡張子でリソースの種類を判断できるよう、「{app}/config/routes.php」に Router::parseExtensions()を追加します。
- xml用のデフォルトレイアウトビューファイル「{app}/views/layouts/xml/default.ctp」を追加します。
- データをXML出力するためのビューファイル「{app}/views/posts/xml/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 | <?php echo $xml ->header(); ?> <?php echo $content_for_layout ; ?> |
cake bake project すると、{app}/views/layouts に email,js,json,rss,xmlのフォルダーが 自動的に作成されるので必要はないがzipを展開しただけなら無いので作成が必要。
1 2 3 | <?php echo $this ->Xml->seeialize( $posts ); ?> |
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 == 'xml' ) { Configure::write( 'debug' , 0); } } function index() { if ( $this ->RequestHandler->ext == 'xml' ) { $posts = $this ->Post->find( 'all' , array ( 'limit' => 20, 'order' => 'Post.created' )); $this ->set(compact( 'posts' )); } } } ?> |