Hey, My recommendation would be to look at the solution that jrbasso has created for CakePHP 2.0: https://github.com/jrbasso/json_plugin It has extensive installation and usage information, making it dead easy to use.
This is a custom view class that allows you to eliminate the need for a view (ctp) file for common json output. If your data is in the right format, and you are just doing a json_encode() in the view, then the section in his readme (see link above) entitled "Directly from your Controller" is what you are after. This has the response generated directly in the controller, and is easy to use. You just need to set a 'json' variable in the controller, and have told your controller to use the 'Json.Json' view class, and you're done! The functionality he has should be able to be back ported to CakePHP 1.3 if you are using that. Cheers, Graham Weldon http://grahamweldon.com e. gra...@grahamweldon.com p. (+61) 0407 017 293 Skype: grahamweldon On Tuesday, 15 November 2011 at 11:19 AM, Shukuboy wrote: > Hi, > > I've been trying to find the best way to share a basic JSON View > across my application. The view is quite simple and only contains : > > <?php echo json_encode($response) ; ?> > > The view is used across the application thorough a utility method in > the AppController : > > protected function renderJSONResponse($response) { > $this -> RequestHandler -> setContent('json', 'application/json'); > $this -> set('response', $response); > $this -> render('json'); > } > > Only problem now is having to re-create the same view, which doesn't > take more than 5 seconds everytime, but is not very DRY, and is messy > and redundant. > > A quick and dirty solution which works is to put the view in a common > folder under the View folder and render it using : > > $this -> render('../Common/json'); > > Just wondering if : > a) There is a better way of achieving the same thing. > b) There are any objections against doing this or any reasons why its > not a good idea ? > > Cheers, > Shuku. > > -- > Our newest site for the community: CakePHP Video Tutorials > http://tv.cakephp.org > Check out the new CakePHP Questions site http://ask.cakephp.org and help > others with their CakePHP related questions. > > > To unsubscribe from this group, send email to > cake-php+unsubscr...@googlegroups.com For more options, visit this group at > http://groups.google.com/group/cake-php > > -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php