CakePHP Extensions and Layouts

I have a controller method that has been processing JSON requests for a long time analyzing this extension, but now I need to open it for the ajax cross domain, so I would like to suggest a JSONP option by analyzing this extension. I have already updated the file routes.php:

Router::parseExtensions( 'json', 'jsonp' );

So far so good, but happiness ends when the results turn out. Although the extension .jsonautomatically selects the layout json/default.ctp, the content .jsonpcontinues to accept a custom layout by default (and all its unnecessary HTML content). I tried to use RequestHandler::setContent()to set the content type of the response to both jsonand to js, but this is not like calling a call to the given layout directory.

Does anyone know what determines which layout directory for specific content? I tried creating jsonp/default.ctp, and I tried creating a layout js/default.ctpwith my JSONP result, but nothing seems to be happening. I just get normal by default.

Any insight into how extensions / content types are mapped to these layout directories would be greatly appreciated.

+5
source share
1 answer

I temporarily solved this by explicitly setting the value layoutPath:

$this->layoutPath = $params['url']['ext'];

This seems like one of those things where there should be a better solution, but maybe that is. I am going to leave the question open to some in the hope that someone else has a solution that includes Cake automagic.

+1
source

All Articles